我有一个MEAN API,我试图更新嵌套在我的文档中的对象。首先,我查询用户配置文件,然后在子文档/嵌套对象中设置属性。
我在另一个终点上做了同样的实施,这是有效的。但由于某些原因,当调用此端点时,我在控制台中收到以下错误:
" TypeError:profile.save不是函数"
以下是应该更新嵌套"共享"的代码。对象:
Profile.find({'contacts.emails.email_address':req.body.invited_email}, function(err, profile){
if(err)
profile[0].contacts.shared.first_name = req.body.first_name;
profile[0].contacts.shared.last_name = req.body.last_name;
profile[0].contacts.shared.initial = req.body.initial;
profile[0].contacts.shared.linkedin = req.body.linkedin;
profile[0].contacts.shared.facebook = req.body.facebook;
profile[0].contacts.shared.pinterest = req.body.pinterest;
profile[0].contacts.shared.twitter = req.body.twitter;
profile[0].contacts.shared.emails = req.body.emails;
profile[0].contacts.shared.phones = req.body.phones;
profile[0].contacts.shared.addresses = req.body.addresses;
profile.save(function(err){
if(err)
res.send(err);
res.json(contact);
});
}
我仍然很擅长使用Node / Express和Mongo开发API,所以我确信这是一件我不想做的简单事情。
感谢任何帮助!!
更新:
这是我的"个人资料"架构。 "个人资料:contacts:shared"部分显示了配置文件的所有者共享的信息以及我的代码尝试更新的部分。
{
"_id":"59474bc3478604a75e1980e2",
"initial":"D",
"last_name":"Smith",
"first_name":"John",
"owner_id":"59474b92478604a75e1980e1",
"__v":25,
"linkedin":"jsmith",
"facebook":"jsmith",
"contacts":[
{
"shared":{
"last_name":"Smith",
"first_name":"John",
"_id":"5949fc8859e8b0c7dd0ca666",
"businesses":[
],
"addresses":[
{
"_id":"59474c5c478604a75e1980ea",
"address_type":"personal",
"zip":"98119",
"state":"WA",
"city":"Seattle",
"address":"102 Hallo Drive"
}
],
"phones":[
{
"_id":"5947521e20b33aa8946c9734",
"phone_type":"home",
"phone_number":"2067879999"
}
],
"emails":[
{
"_id":"59474c1f478604a75e1980e8",
"email_address":"looking@filter.com",
"email_type":"business"
}
]
},
"last_name":"Jones",
"first_name":"Tom",
"_id":"5949fc8859e8b0c7dd0ca664",
"businesses":[
],
"addresses":[
],
"phones":[
],
"emails":[
{
"email_type":"home",
"email_address":"tsom@jones.com",
"_id":"5949fc8859e8b0c7dd0ca665"
}
]
}
],
"addresses":[
{
"address":"102 Hallo Drive",
"city":"Seattle",
"state":"WA",
"zip":"98119",
"address_type":"personal",
"_id":"59474c5c478604a75e1980ea"
}
],
"phones":[
{
"phone_number":"2067879999",
"phone_type":"home",
"_id":"5947521e20b33aa8946c9734"
}
],
"emails":[
{
"email_type":"personal",
"email_address":"chris@chrisnakea.com",
"_id":"59474c01478604a75e1980e6"
},
{
"email_type":"business",
"email_address":"smart@guy.com",
"_id":"59474c0e478604a75e1980e7"
}
]
}