对象数组返回对象Object

时间:2016-03-16 06:09:50

标签: javascript arrays mongodb javascript-objects

我正在尝试向对象添加一个对象数组,但是当我在控制台中查看它时,它看起来像[object Object]

代码:

var myObj = new models.myObjModel({
     Name: req.body.Name,
     Id: req.body.Id,
     Balances: [ {type: req.body.Balances[0].type, balance: req.body.Balances[0].balance},
                 {type: req.body.Balances[1].type, balance: req.body.Balances[1].balance}
               ],
     Birthday: req.body.birthday,
     Gender: req.body.gender
  });
  console.log(this_Kiddy);

我明白了:

{ Name: 'Ian',
  Id: '569496f45293a6170c3fe059',
  Birthday: Fri Dec 31 1999 16:00:00 GMT-0800 (PST),
  Gender: 'boy',
  _id: 56e8f2c0ef7b851539f481f0,
  Balances: [ '[object Object]', '[object Object]' ] }

Mongo插件如下所示:

Mongoose: person.insert({ __v: 0, Balances: [ '[object Object]', '[object Object]' ], _id: ObjectId("56e8f2c0ef7b851539f481f0"), Gender: 'boy', Birthday: new Date("Sat, 01 Jan 2000 00:00:00 GMT"), Id: '569496f45293a6170c3fe059', Name: 'Ian' }) {} 

当我选择余额时,我得到[对象对象],不是很有用。我怎样才能真正获得余额? 谢谢你提前。

编辑:

这是以前的日志声明:

  console.log(req.body.Balances);
  console.log(req.body.Balances[0].type);
  console.log(req.body.Balances[0].balance);
  console.log(req.body.Balances[1].type);
  console.log(req.body.Balances[1].balance);

并输出:

 [ { type: 'Balance1', balance: 2 },
   { type: 'Balance2', balance: 20 } ]
 Balance1
 2
 Balance2
 20

EDIT2:

var myObj = mongoose.Schema({  
  Name: { type: String },
  Id: { type: String },  
  Balances: [{
               type: String,
               balance: Number
            }],
  Birthday: {type: Date},
  Gender: {type: String}
}); 

0 个答案:

没有答案