這是一個新問題,因爲還有一些關於JavaScript的小東西讓我感到困惑。下劃線_each不向對象元素添加屬性
我有以下代碼:
db.units.find({}, { _id: 1, type: 1, name: 1 }).sort({ type: 1, name: 1}).exec(function(err, units) {
if(err) return next(err);
_.each(units, function(u, i) {
this[i]["available"] = !_.contains(_.pluck(_.pluck(req.workorder.units, 'unit'), 'name'), u.name);
console.log(this[i].available);
console.log(this[i]);
console.log(_.extend(this[i],{available:this[i].available}));
}, units);
return res.render('workorder/show', {
workorder: req.workorder,
invoices: db.invoices.find({ workorders: req.params.id }),
units: units
});
});
這裏是結果:
真正 {_id:513bd489b0e7e2634b60dc47, 名稱: '視覺油底殼檢查UST-22C NC', 類型: '服務'}
它正在記錄可用的屬性,但不在對象本身,這是爲什麼?
相反的console.log的'()',把一個'調試器:對象只能與document.toObject的使用()
http://mongoosejs.com/docs/api.html#document_Document-toObject
Likeso改性那裏。然後,當您運行它時,它將停止在JavaScript調試器中,您可以交互式瀏覽對象數據。當你不確定數據格式時,這可以使它更容易。 –
@MichaelGeary這段代碼碰巧在服務器端運行 –
它可以是「this [i]」對象有其toString()方法被覆蓋 – nilgun