在Mongoose更新查询期间,JavaScript对象元素缺失

我试图更新一个新的数据字段到一个现有的文件,但一些数据不更新。

在我的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(数据)结果

  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(数据)导致我的terminal

 { 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

您需要在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 } })