我有以下帖子:
.post(function(req, res) {
member.findById(req.params.member_id, function(err, result) {
if (err)
return (err);
new_address = {
street1 : req.body.street1,
street2 : req.body.street2,
City : req.body.City,
State : req.body.State,
Zip : req.body.Zip,
Lat : req.body.Lat,
Lng : req.body.Lng
};
if(result.address === undefined){
result.address.push(new_address);
}
else{
result.address = [new_address];
}
result.save(function(err) {
if (err)
return res.send(err);
res.json({ message: 'Address created!!!' });
});
});
});
在尝试result.save()
电话时出于某种原因,我收到此错误:
{
"message": "Cast to undefined failed for value \"[object Object]\" at path \"address\"",
"name": "CastError",
"value": [
{
"address_type": "Home",
"street1": "2018 Elliot St",
"City": "Alexandria",
"State": "LA",
"Zip": 71301,
"Lat": 12,
"Lng": 32
}
],
"path": "address"
}
我的Address模式是Member Schema的子模式,如下所示:
var mongoose = require('mongoose'),
Schema = mongoose.Schema
var AddressSchema = Schema({
Address_type : String,
street1 : String,
street2 : String,
City : String,
State : String,
Zip : Number,
Lat : Number,
Lng : Number
});
module.exports = mongoose.model('Address', AddressSchema);
以下是成员架构:
var mongoose = require('mongoose'),
Schema = mongoose.Schema
var Address = require('./address');
var Award = require('./award');
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 : [Award],
granted: { type: Date, default: Date.now }
}
});
module.exports = mongoose.model('Member', MemberSchema);
我在这里苦苦挣扎。如果有人能够理解为什么我会收到这个错误,我将不胜感激。
非常感谢
答案 0 :(得分:0)
这里的问题是您在Address
中定义嵌入式架构时尝试使用模型(MemberSchema
)。您在定义embedded docs时应该使用架构,而不是模型。
var Address = require('./address');
这是一个模型,根据相关文件中的导出语句。要么导出架构,要么更好,您可以从模型访问架构,如下所示:Address.schema
因此,请在MemberSchema
定义中更改此行
address: [Address],
到这个
address: [Address.schema],
在其他任何地方,您也可以像使用award : [Award.schema],