gpt4 book ai didi

javascript - 如何在 Mongoose 模式中添加子文档

转载 作者:可可西里 更新时间:2023-11-01 10:03:37 25 4
gpt4 key购买 nike

我正在尝试在 node.js/Express 的 mongoose 模式中创建一个子文档。

我有两个模式:成员和地址

成员(member).js

// app/models/member.js

// load mongoose since we need it to define a model
var mongoose = require('mongoose'),
Schema = mongoose.Schema

var Address = require('./address');

var MemberSchema = Schema({
FName : String,
LName : String,
address : [Address],
phone : {
type : String,
number : String
},
email: String,
gender: String,
DOB: Date,
rank : {
level : String,
updated: { type: Date, default: Date.now }
},
Awards : {
personal : Boolean,
award : { type : Schema.Types.ObjectId, ref : 'Award' },
granted: { type: Date, default: Date.now }
}
});


module.exports = mongoose.model('Member', MemberSchema);

地址.js

// app/models/address.js

// load mongoose since we need it to define a model
var mongoose = require('mongoose'),
Schema = mongoose.Schema

var AddressSchema = Schema({
type : String,
street1 : String,
street2 : String,
City : String,
State : String,
Zip : Number,
Lat : Number,
Lng : Number
});


module.exports = mongoose.model('Address', AddressSchema);

我的意图是创建一个成员,然后添加一个地址。成员工作的 POST 调用。但是,当执行 POST 调用地址时,它失败说未定义,我找不到错误的位置。我希望了解如何更好地将地址添加到我的成员模式。

路由.js

  app.route('/api/member')

.get(function(req, res) { ...

.post(function(req, res) {
var new_member = new member();
new_member.FName = req.body.fname;
new_member.LName = req.body.lname;
new_member.DOB = req.body.DOB;
new_member.email = req.body.email;
new_member.gender = req.body.gender;

new_member.save(function(err) {
if (err)
res.send(err);

res.json({ message: 'Member Created!' });
});
});

app.route('/api/member/:member_id/address')

.post(function(req, res) {
member.findById(req.params.member_id, function(err, member) {
if (err)
return (err);

new_address = new address();
new_address.type = req.body.atype;
new_address.street1 = req.body.street1;
new_address.street2 = req.body.street2;
new_address.City = req.body.City;
new_address.State = req.body.State;
new_address.Zip = req.body.Zip;
new_address.Lat = req.body.Lat;
new_address.Lng = req.body.Lng;
console.log(new_address);

member.address.push(new_address);

res.json({ message : "Address added!!" });

});
});

就像我说的,添加成员很容易。添加地址会导致以下错误:

/Users/arcee123/projects/MCARS/node_modules/mongoose/lib/utils.js:419
throw err;
^
TypeError: undefined is not a function
at Array.MongooseArray._cast (/Users/arcee123/projects/MCARS/node_modules/mongoose/lib/types/array.js:108:30)
at Object.map (native)
at Array.MongooseArray.push (/Users/arcee123/projects/MCARS/node_modules/mongoose/lib/types/array.js:262:23)
at Promise.<anonymous> (/Users/arcee123/projects/MCARS/app/routes.js:108:24)
at Promise.<anonymous> (/Users/arcee123/projects/MCARS/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
at Promise.emit (events.js:107:17)
at Promise.emit (/Users/arcee123/projects/MCARS/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
at Promise.fulfill (/Users/arcee123/projects/MCARS/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
at /Users/arcee123/projects/MCARS/node_modules/mongoose/lib/query.js:1833:13
at model.Document.init (/Users/arcee123/projects/MCARS/node_modules/mongoose/lib/document.js:251:11)
15 Jun 15:45:49 - [nodemon] app crashed - waiting for file changes before starting...

我哪里错了?谢谢

更新 1:地址推送的 .post 调用现在看起来像这样:

.post(function(req, res) {
member.findById(req.params.member_id, function(err, member) {
if (err)
return (err);

new_address = new address();
new_address.type = req.body.atype;
new_address.street1 = req.body.street1;
new_address.street2 = req.body.street2;
new_address.City = req.body.City;
new_address.State = req.body.State;
new_address.Zip = req.body.Zip;
new_address.Lat = req.body.Lat;
new_address.Lng = req.body.Lng;
console.log(new_address);

if(member.address === undefined){
member.address.push(new_address);
}
else{
member.address = [new_address];
}

member.save(function(err) {
if (err)
res.send(err);

res.json({ message: 'Address created!!!' });
});

});

});

最佳答案

由于当您从数据库中检索模型时 Mongoose 不需要地址字段,因此不会定义该字段。因此,您将无法添加地址字段。你应该检查它是否存在

if(member.address !== undefined){
member.address.push(new_address);
}
else{
member.address = [new_address];
}

编辑:然后您还必须将其保存回数据库中。 (也可以看看update函数)

关于javascript - 如何在 Mongoose 模式中添加子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30854692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com