Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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 用lowdb替换JSON文件中对象中的数组_Javascript_Json - Fatal编程技术网

Javascript 用lowdb替换JSON文件中对象中的数组

Javascript 用lowdb替换JSON文件中对象中的数组,javascript,json,Javascript,Json,我一直在努力更新JSON文件。到目前为止,我可以在某些数组中添加新对象。但现在我想删除一个。这是我的代码: var index = request.body.index; var items = db.get("users") .filter({ "authKey": request.body.authKey }) .map("cart") .map("items") .value(); items.splice(index, 1); var cart = {

我一直在努力更新JSON文件。到目前为止,我可以在某些数组中添加新对象。但现在我想删除一个。这是我的代码:

var index = request.body.index;
var items = db.get("users")
    .filter({ "authKey": request.body.authKey })
    .map("cart")
    .map("items")
    .value();

items.splice(index, 1);

var cart = {
    "items": items,
    "numbersOfItems": items.length
};

db.get("users")
    .assign({ items: items })
    .write();

// db.get("users")
//     .filter({"authKey": request.body.authKey})
//     .set("cart", cart)
//     .value();

// db.get("users")
//     .filter({"authKey": request.body.authKey})
//     .map("cart")
//     .assign("items", items)
//     .write();`
JSON是:

users : [{
  "username": "shop",
  "passHash": "something",
  "authKey": "something",
  "cart": {
    "items": [
      {
        "title": "Product 1",
        "price": 1000,
        "img": "url",
        "description": "Amazing! WOW!"
      }
    ]
  }
}]

这里的问题是什么?我想用此代码删除产品1,但它不起作用。您是否检查了您得到的
request.body.index
的值?是的,它是正确的索引。response.body是
{authKey:'NWTRssDxY_qrDpZvnB_egwvfmlxzawe+Cgu=kkbvvvm AUqGn%gdmC+=tu',index:0}{authKey:'NWTRssDxY_qrDpZvnB_egwvfmlxzawe+Cgu=kkbvvm AUqGn%gdmC+=tu',仍然没有结果。