【发布时间】:2015-07-20 17:48:21
【问题描述】:
我正在尝试将新的数据字段更新到现有文档中,但某些数据没有更新。
在我的 AngularJS 控制器中:
$scope.tellUsMore = function(){
var data = {
businessPhone:$scope.usersData.phone,
businessEmail:$scope.usersData.email,
businessFb:$scope.usersData.fb,
businessTwitter:$scope.usersData.twitter,
businessInstagram:$scope.usersData.instagram,
businessAboutUs:$scope.usersData.aboutUs,
businessTags:$scope.tags,
businessFeatures:$scope.features,
businessLocation:$scope.usersData.location,
businessPriceRange:$scope.usersData.priceRange,
businessPreparationTimeRange:$scope.usersData.preparationTimeRange
}
console.log(data); //result below
Account.updateProfile(data)
.success(function() {
alert("DONE")
})
.error(function(error) {
console.log(error)
});
}
chrome 控制台选项卡上的 console.log(data) 结果
Object
businessAboutUs: "LOL"
businessEmail: "example@gmail.com"
businessFb: undefined
businessFeatures: Array[5]
businessInstagram: undefined
businessLocation: Object
businessPhone: "0123456789"
businessPreparationTimeRange: 2
businessPriceRange: 2
businessTags: Array[2]
businessTwitter: undefined
__proto__: Object
在我的 Node.js 服务器中
this.updateProfile = function(req, res, next){
var data = req.body;
console.log(data)//result below
User.update(req.user, {$set: { businessDetails:data }}, {upsert: true}, function(err,user){
res.status(200);
});
}
console.log(data) 结果在我的终端中
{ businessPhone: '0123456789',
businessEmail: 'example@gmail.com',
businessAboutUs: 'LOL',
businessTags:
[ { name: 'Marina Augustine',
email: 'm.augustine@exampleas.com',
image: 'http://lorempixel.com/50/50/people?0',
_lowername: 'marina augustine' },
{ name: 'Oddr Sarno',
email: 'o.sarno@exampleas.com',
image: 'http://lorempixel.com/50/50/people?1',
_lowername: 'oddr sarno' } ],
businessFeatures:
[ { id: 1, title: 'Do you accept credit card ?', selected: true },
{ id: 2,
title: 'Do you accept table reservation ?',
selected: false },
{ id: 3,
title: 'Do you provide Wi-Fi for your customer ?',
selected: false },
{ id: 4, title: 'Is your product Halal ?', selected: true },
{ id: 5,
title: 'Do you provide parking for your customer ?',
selected: true } ],
businessLocation: { latitude: 3.1168450143582223, longitude: 101.60914228515628 },
businessPriceRange: 2,
businessPreparationTimeRange: 2 }
然而,这就是我得到的——只有businessLocation 更新为businessDetails,而businessLocation 甚至都不完整。
> db.users.find().pretty()
{
"_id" : ObjectId("554eb9a8bfa096290c9efa46"),
"companyName" : "t and co",
"email" : "example@gmail.com",
"password" : "$2a$10$79b.XztwEXgdCPDxTkg4ieICSkYyKw4uXG/2E0WShSZxXVdGdwObm",
"dateJoined" : ISODate("2015-05-10T01:51:36.120Z"),
"accountVerified" : false,
"locationVerified" : false,
"__v" : 0,
"businessDetails" : {
"businessLocation" : {
}
}
}
>
user 的架构
var userSchema = new db.Schema({
email: { type: String, unique: true, lowercase: true },
password: { type: String, select: false },
companyName: String,
locationVerified: { type:Boolean, default:false},
accountVerified: { type:Boolean, default:false},
dateJoined: {type:Date, default:Date.now}
})
req.user的值
554eb9a8bfa096290c9efa46 这是mongodb中的objectID
【问题讨论】:
-
@JohnnyHK 注意到,我只是将屏幕截图替换为实际文本。
-
@JohnnyHK hi 添加了用户架构和 req.user 的内容
-
如果您希望能够更新它,您需要在
userSchema中定义businessDetails子文档。 -
@JohnnyHK 好的,我可以知道我应该输入哪种数据类型吗?因为它包含对象数组和数字。我已将 upsert 设置为 true,如果没有找到,它不会更新新字段吗?
-
好的,看我的回答。您不需要
{upsert: true},它仅适用于整个文档。
标签: javascript angularjs node.js mongodb mongoose