JavaScript Mongoose 更新查询期间缺少对象元素

JavaScript object element is missing during Mongoose update query

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

在我的 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)
        });
}

console.log(数据)结果在 chrome 控制台选项卡上

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

{ 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 更新为 businessDetailsbusinessLocation 甚至不完整。

> db.users.find().pretty()
{
    "_id" : ObjectId("554eb9a8bfa096290c9efa46"),
    "companyName" : "t and co",
    "email" : "example@gmail.com",
    "password" : "ab.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

中的对象 ID

如果您希望能够更新它,您需要在 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
  }
})