嗨,我正在解决这个问题,它来自RefLink。
您可以创建这样的方案来映射您的数据
const User = mongoose.model('Story', userSchema);
module.exports = User;
const mongoose = require('mongoose');
let Schema = mongoose.Schema;
const userSchema = new Schema({
UserID: {
type: mongoose.Schema.Types.Mixed,
},
User_Info: {
First_Name: {
type: String,
},
Last_Name: {
type: String,
},
Current_Address: {
type: String,
},
Email_Address: {
type: String,
},
},
Phone_Numbers: [{
Home_Phone: {
type: Number,
},
Work_Phone: {
type: Number,
},
Cell_Phone: {
type: Number,
},
Phone_verified: [{
Home: Boolean,
Work: Boolean,
Cell: Boolean,
}],
}],
})
const User = mongoose.model('User', userSchema);
module.exports = User;
而 API 路由可能看起来像这样
app.post('/api/user', function(req, res) {
User.create({
UserID: req.body.userid,
User_Info: req.body.userinfo,
First_Name: req.body.firstname,
Last_Name: req.body.lastname,
Current_Address: req.body.currentaddress,
Email_Address: req.body.emailaddress,
Phone_Numbers: req.body.phonenumbers,
Home_Phone: req.body.homephone,
Work_Phone: req.body.workphone,
Cell_Phone: req.body.cellphone,
Phone_Verified:
req.body.phoneverified,
Home: req.body.home,
Work: req.body.work,
Cell: req.body.cell,
}).then(user => {
res.json(user)
});
});