Guest User

mongoosearray

a guest
Mar 20th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ****** Schema ******
  2. var Account = new Schema({
  3.     username: {type: String, unique:true},
  4.     password: String,
  5.     profile: {
  6.         name: String,
  7.         email: String,
  8.         phone: [{type: String, number: Number }],
  9.         address: [{type: String, address: String }]
  10.     },
  11. });
  12.  
  13. // ****** find and update ******
  14.     Account.findOne({ username: req.username }, function ( err, account ) {
  15.         if ( err )
  16.             res.json ({success: false, err: err });
  17.         else {
  18.             if ( req.body.name ) account.profile.name = req.body.name;
  19.             if ( req.body.email ) account.profile.email = req.body.email;
  20.            
  21.             if ( req.body.phone ) {
  22.                 console.log (( JSON.parse ( req.body.phone ) ));
  23.                 account.profile.phone = JSON.parse ( req.body.phone );      //THIS THROWS ERROR
  24.             }
  25.  
  26.             account.save ( function ( err, account ){
  27.                 if ( err ) res.json ({success: false, err: err });
  28.                 res.json ({ success: true, account: account });        
  29.             });
  30.            
  31.         }
  32.     });
  33.  
  34. // ERROR RETURNED TO POSTMAN
  35. {
  36.   "success": false,
  37.   "err": {
  38.     "message": "accounts validation failed",
  39.     "name": "ValidationError",
  40.     "errors": {
  41.       "profile.phone": {
  42.         "message": "Cast to Array failed for value \"[object Object]\" at path \"profile.phone\"",
  43.         "name": "CastError",
  44.         "kind": "Array",
  45.         "value": [
  46.           {
  47.             "device": "Mobile",
  48.             "number": "9790846755"
  49.           }
  50.         ],
  51.         "path": "profile.phone"
  52.       }
  53.     }
  54.   }
  55. }
Add Comment
Please, Sign In to add comment