我有以下插入成分的功能:
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]"
}
]
]
我没有想法如何解决它,也许任何人都会有想法? 当我在名称[i] .toString上打字时,例如在推送之前它具有String
的类型答案 0 :(得分:0)
您正面临这个问题,因为您正在尝试将对象转换为字符串,因为这样的字符串显示为"[object Object]"
。
您需要在上面的评论中告诉您从对象获取ingr_name
。因此,不是将整个对象转换为字符串,而是从对象中提取ingr_name
,如此
names->ingr_name