gpt4 book ai didi

Mongoose 更新查询期间 JavaScript 对象元素丢失

转载 作者:太空宇宙 更新时间:2023-11-04 00:55:15 25 4
gpt4 key购买 nike

我正在尝试将新的数据字段更新到现有文档中,但某些数据未更新。

在我的 AngularJS Controller 中:

$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" : {

}
}
}
>

用户的架构

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

最佳答案

如果您希望能够更新,则需要在 userSchema 中定义 businessDetails 子文档:

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},
businessDetails: {
businessPhone: String,
businessEmail: String,
businessAboutUs: String,
businessTags: [],
businessFeatures: [],
businessLocation: {
latitude: Number,
longitude: Number
},
businessPriceRange: Number,
businessPreparationTimeRange: Number
}
})

关于Mongoose 更新查询期间 JavaScript 对象元素丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30147077/

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