如何在Node.js中更新JSON对象值

时间:2019-04-26 08:42:07

标签: node.js json angular

我的Javascript正在与以下2个JSON文件进行交互:

readers.json:

[
    {
    "readerID": 1,
    "name": "Maria",
    "weeklyReadingGoal": 350,
    "totalMinutesRead": 5600,
    "bookID": 65
    },
    {
    "readerID": 2,
    "name": "Daniel",
    "weeklyReadingGoal": 210,
    "totalMinutesRead": 3000,
    "bookID": 65
    }
]

Books.json

[
    {
    "bookID": 65,
    "title": "Goodnight Moon",
    "author": "Margaret Wise Brown",
    "publicationYear": "1953"
    },
    {
    "bookID": 75,
    "title": "Winnie-the-Pooh",
    "author": "A. A. Milne",
    "publicationYear": "1926"
    }
]

目前,我可以从 book.json

中删除一本书

当我删除一本书,并且该书的bookID存在于 reader.json 对象中时,我想将该ID更新为0。

例如,如果我删除bookID = 65的一本书,我想将 reader.json 中等于65的所有bookID更改为null。

下面是我的代码,如果需要,我可以提供其他代码:

Books.js:

var data = getBookData();

var pos = data.map(function (e) {
  return e.bookID;
}).indexOf(parseInt(req.params.id, 10));

if (pos > -1) {
  data.splice(pos, 1);
} else {
  res.sendStatus(404);
}

saveBookData(data);

var readerData = getReaderData();
var bookID = req.params.id;
console.log('BOOK ID - ' + bookID); // prints: BOOK ID - 47
readerData.forEach(function (x) {
  if (x.bookID === bookID) {
    x.bookID = null;
  }
  else {
    console.log('Deleted book was not associated with any reader');
    console.log('BOOK ID in else - ' + x.bookID); // prints: BOOK ID in else - 47
  }
});

saveReaderData(readerData);
res.sendStatus(204);
});

function getReaderData() {
    var data = fs.readFileSync(readerFile, 'utf8');
    return JSON.parse(data);
}

运行此代码时,该书将从 books.json 中删除,但是 readers.json 中匹配的bookID保持不变。

此外,在上面的else块中,匹配的bookID正在被记录,但是由于某些原因,代码没有流入if块中。

1 个答案:

答案 0 :(得分:0)

您可以在阅读器上循环播放并检查bookID。这就是你想要的吗?

您的错误是在数组上使用indexOf,因为它只会向您返回第一个匹配项。

const readerData = [{
    "readerID": 1,
    "name": "Maria",
    "weeklyReadingGoal": 350,
    "totalMinutesRead": 5600,
    "bookID": 65,
  },
  {
    "readerID": 1,
    "name": "Maria",
    "weeklyReadingGoal": 350,
    "totalMinutesRead": 5600,
    "bookID": 70,
  },
  {
    "readerID": 2,
    "name": "Daniel",
    "weeklyReadingGoal": 210,
    "totalMinutesRead": 3000,
    "bookID": 65,
  },
];

const bookID = 65;

// Treat every reader
readerData.forEach((x) => {
  // If the bookID is the one we are looking for, set it as null
  if (x.bookID === bookID) {
    x.bookID = null;
  }
});

console.log(readerData);


所以。应用于您的代码

var readerFile = 'server/data/readers.json';

router.route('/')
  .delete(function(req, res) {
    var readerData = getReaderData();

    var bookID = parseInt(req.params.id, 10);

    // Have we found any occurence of the bookID ?
    var found = false;

    // Treat every reader
    readerData.forEach(function(x) {
      // If the bookID is the one we are looking for, set it as null
      if (x.bookID === bookID) {
        x.bookID = null;

        found = true;
      }
    });

    // If we haven't found any occurence, returns an error
    if (!found) {
      res.sendStatus(404);

      return;
    }

    // Save the changes
    saveReaderData(readerData);

    res.sendStatus(204);
  });

function getReaderData() {
  var data = fs.readFileSync(readerFile, 'utf8');

  return JSON.parse(data);
}