我通过axios post将一个用户对象和一个简单的字符串发布到后端,然后希望使用mongoose将这些数据存储在MongoDB中。如果我发送整个东西没有浏览器字符串,即只有用户对象并在后端处理它,一切工作。对象具有除浏览器属性之外的所有属性。我后来加了这个。那么,如何保存用户对象1:1,同时保存其他属性,比如这里的浏览器属性?实际上保存的只是浏览器字符串
使用mongoose保存新用户的功能:
//CREATE NEW USER
export const newUser = async (req, res, next) => {
try {
const user = new User({ ...req.body.userD, browser: req.body.browser });
const savedUser = await user.save();
const myPromise = new Promise((resolve, reject) => {
resolve(savedUser);
});
myPromise.then(() => {
res.status(200).json("User saved");
});
} catch (error) {
next(err);
}
};
axios post:
let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'http://localhost:3999/server/user/new',
headers: {
'Content-Type': 'application/json'
},
userD: data,
browser:browser
};
await axios.request(config)
.then((result) => {
res.end(result.data)
})
用户型号:
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema(
{
asn: {
type: String,
},
city: {
type: String,
},
continent_code: {
type: String,
},
country: {
type: String,
},
country_area: {
type: Number,
},
country_calling_code: {
type: String,
},
country_capital: {
type: String,
},
country_code: {
type: String,
},
country_code_iso3: {
type: String,
},
country_name: {
type: String,
},
country_population: {
type: Number,
},
country_tld: {
type: String,
},
currency: {
type: String,
},
currency_name: {
type: String,
},
in_eu: {
type: Boolean,
},
ip: {
type: String,
},
languages: {
type: String,
},
latitude: {
type: Number,
},
longitude: {
type: Number,
},
network: {
type: String,
},
org: {
type: String,
},
postal: {
type: Number,
},
region: {
type: String,
},
region_code: {
type: String,
},
timezone: {
type: String,
},
utc_offset: {
type: String,
},
version: {
type: String,
},
browser: {
type: String,
},
},
{ timestamps: true }
);
export default mongoose.model("User", UserSchema);
更新
这是发送到Express服务器的数据:
{"data ":{"ip":"2003年:c4:www.example.com "," network ":" 2003年:xxx","版本":“IPv6”、“城市”:"xxxx "," region ":" xxxx","region_code":"BW "," country ":" DE","country_name":"德国","国家代码":" DE","country_code_iso3":"DEU "," country_capital ":" Berlin","country_tld":". de "," continent_code ":" EU","in_eu":true,"postal":"xxxx "," latitude":xxxx,“经度”:x. xx," timezone":"欧洲/柏林"," utc_offset":"+0200"," country_calling_code":“+49”,“货币”:" EUR"," currency_name":" Euro"," languages":" de"," country_area":357021," country_population":82927922,“asn”:" AS3320"," org":" Deutsche Telekom AG "}," browser":浏览器f720.xxx ", "network":"2003:xxx", "version":"IPv6", "city":"xxxx", "region":"xxxx", "region_code":"BW", "country":"DE", "country_name":"Germany", "country_code":"DE", "country_code_iso3":"DEU", "country_capital":"Berlin", "country_tld":".de", "continent_code":"EU", "in_eu":true, "postal":"xxxx", "latitude":xxxx, "longitude":x.xx, "timezone":"Europe/Berlin", "utc_offset":"+0200", "country_calling_code":"+49", "currency":"EUR", "currency_name":"Euro", "languages":"de", "country_area":357021, "country_population":82927922, "asn":"AS3320", "org":"Deutsche Telekom AG" }, "browser":"Browser" }
这是保存在mongodb
中的内容
1条答案
按热度按时间lstz6jyr1#
从你的编辑部分,我看到你需要访问“数据”字段,然后传播…