Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更新Mongo文档数组中的对象(在特定索引处)_Javascript_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Javascript 如何更新Mongo文档数组中的对象(在特定索引处)

Javascript 如何更新Mongo文档数组中的对象(在特定索引处),javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我想让用户检查他们是否“购买”了一件杂货,并更新数据库 更具体地说,当用户选中一个框时,我想在一个配料对象上切换一个布尔属性获取的。配料以数组形式存储在GroceryList文档中: 以下是杂货清单的模式 const GroceryListSchema = mongoose.Schema({ createdAt : { type: Date, default: Date.now }, updatedAt : { type: Date }, ingred

我想让用户检查他们是否“购买”了一件杂货,并更新数据库

更具体地说,当用户选中一个框时,我想在一个配料对象上切换一个布尔属性
获取的
。配料以数组形式存储在
GroceryList
文档中:

以下是杂货清单的模式

const GroceryListSchema = mongoose.Schema({
  createdAt         : { type: Date, default: Date.now },
  updatedAt         : { type: Date },
  ingredients       : { type: Array },
  recipes           : { type: Array },
  user              : { type: mongoose.Schema.Types.ObjectId, ref: 'UserSchema', required: true },
}
一种成分如下所示:

{ quantity: '3',
  unit: null,
  ingredient: 'zucchinis, chopped',
  minQty: '3',
  maxQty: '3',
  acquired: false }
我看过很多类似的问题,Mongoose文档和MongoDB文档,我尝试了很多不同的版本,但是我被难住了

前端:

function toggleIngredient(elmt, groceryListId, ingrIdx) {
  $.post('/cart/grocery-list/toggleIngredient', {
    groceryListId,
    ingrIdx,
    newValue: elmt.checked, // if checkbox was checked before toggling it
  })
    .then((res) => {
      console.log(res);
    })
    .catch((err) => {
      console.log(err);
    });
}
后端:

  app.post('/cart/grocery-list/toggleIngredient', (req, res, next) => {
    const { groceryListId, ingrIdx, newValue } = req.body;

    GroceryListSchema.findById(groceryListId, (err, groceryList) => {
      if (err) return next(err);

      // if was unchecked, change to checked & vice-versa
      groceryList.ingredients[ingrIdx].acquired = newValue;

      // save updated grocery list
      groceryList.save().then((updatedList) => {
        console.log(updatedList.ingredients);
      }).catch(error => next(error));
    });
  });
结果/问题: 当我运行上面的代码时,我在回调中成功地看到1个成分的
acquired
属性从
false
->
true
切换

console.log(updatedList.ingredients);

然而,下一次我去拿食品杂货清单时,同样的成分是
acquired=false
。这让我相信
GroceryList
文档实际上并没有在数据库中得到更新。如何修复此问题?

如果直接使用数组元素的索引修改数组元素,mongoose将无法跟踪数组内部的更改

保存前请尝试添加此行

groceryList.markModified(`ingredients.${ingrIdx}.acquired`);