John Lim
John Lim

Reputation: 3109

JavaScript object element is missing during Mongoose update query

I'm trying to update a new field of data into an existed document, but some of the data is not updating.

In my 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)
        });
}

the console.log(data) result on chrome console tab

 Object
    businessAboutUs: "LOL"
    businessEmail: "[email protected]"
    businessFb: undefined
    businessFeatures: Array[5]
    businessInstagram: undefined
    businessLocation: Object
    businessPhone: "0123456789"
    businessPreparationTimeRange: 2
    businessPriceRange: 2
    businessTags: Array[2]
    businessTwitter: undefined
    __proto__: Object

In my Node.js server

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);
      });
}

the console.log(data) result in my terminal

{ businessPhone: '0123456789',
  businessEmail: '[email protected]',
  businessAboutUs: 'LOL',
  businessTags: 
   [ { name: 'Marina Augustine',
       email: '[email protected]',
       image: 'http://lorempixel.com/50/50/people?0',
       _lowername: 'marina augustine' },
     { name: 'Oddr Sarno',
       email: '[email protected]',
       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 }

However, this is what I got – only businessLocation updated to businessDetails, and the businessLocation is not even complete.

> db.users.find().pretty()
{
    "_id" : ObjectId("554eb9a8bfa096290c9efa46"),
    "companyName" : "t and co",
    "email" : "[email protected]",
    "password" : "$2a$10$79b.XztwEXgdCPDxTkg4ieICSkYyKw4uXG/2E0WShSZxXVdGdwObm",
    "dateJoined" : ISODate("2015-05-10T01:51:36.120Z"),
    "accountVerified" : false,
    "locationVerified" : false,
    "__v" : 0,
    "businessDetails" : {
        "businessLocation" : {

        }
    }
}
>

schema for 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}
    })

value of req.user

554eb9a8bfa096290c9efa46 this is an objectID in mongodb

Upvotes: 1

Views: 143

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311955

You need to define the businessDetails subdoc in userSchema if you want to be able to update it:

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
  }
})

Upvotes: 1

Related Questions