【发布时间】:2018-02-21 12:49:39
【问题描述】:
我有以下添加成分的功能:
router.post('/api/recipes/ingredient/post', function (req, res) {
console.log('before for');
var names = req.body.ing_name;
var amounts = req.body.ing_amount;
for (var i = 0; i < names.length; i++) {
var ingredientItem = {ingredient_name: names[i].toString(), amount: amounts[i].toString()};
Recipe.update({name: req.body.name},
{$push: {"ingredients": ingredientItem}},
function (err, res) {
if (err) {
console.log(err)
console.log("ERROR OCCURRED, COULD NOT SAVE USER IN DATABASE");
}
else {
console.log("USER SUCCESSFULLY MODIFIED IN DATABASE");
}
});
}
});
但在收藏中它最终是:
"ingredients" : [
[
{
"_id" : ObjectId("59b8726b4de01a2958511871"),
"amount" : "[object Object]",
"ingredient_name" : "[object Object]"
}
],
[
{
"_id" : ObjectId("59b8726b4de01a2958511872"),
"amount" : "[object Object]",
"ingredient_name" : "[object Object]"
}
]
]
我没有办法解决它,也许有人会有想法? 例如,当我在 names[i].toString 上执行 typeof 时,在推送它的类型为 String
【问题讨论】:
-
控制台日志
names[i].toString()会得到什么 -
那个对象 Object thingy 现在对我来说更没有意义了
-
那么这不是插入问题,问题出在其他地方。现在请控制台日志
name[i]并粘贴它的结果,以便我可以看到正在转换为字符串的内容 -
{ingr_name: 'bleble'} 但为什么呢?
-
您正在将对象转换为字符串,因此显示
"[object Object]"
标签: arrays node.js mongodb mongoose mean-stack