【发布时间】:2013-10-03 06:55:48
【问题描述】:
这是一个新手问题,因为仍然有一些关于 javascript 的小问题让我感到困惑。
我有以下代码:
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, name: '视觉油底壳检查 UST-22C NC', 类型:“服务”}
它正在记录可用的属性,但不是单独在对象中,这是为什么呢?
【问题讨论】:
-
在里面放一个
debugger;声明而不是console.log()。然后,当您运行它时,它会在 JavaScript 调试器中停止,您可以交互地探索对象数据。当您不确定数据格式时,这会更容易。 -
@MichaelGeary 此代码恰好在服务器端运行
-
会不会是 "this[i]" 对象的 toString() 方法被覆盖了
-
@nilgundag 这不是问题
-
不要使用
this[i],而只使用u!
标签: javascript mongoose underscore.js