为什么findOneAndUpdate()不更新文档?

时间:2020-10-10 01:09:20

标签: node.js mongodb mongoose

我刚开始使用猫鼬,但无法更新文档。我的代码找到了文档,但没有更新属性,最终删除了该文档。我在做什么错了?

<!DOCTYPE html>

<html>
  <head>
    <title>Story Time</title>
  </head>

  <body>
    <h1>Story Time</h1>

    <table style="margin-left: auto; margin-right: auto">
      <tr>
        <td>Name:</td>
        <td><input type="text" id="NameBox" value="" size="20" /></td>
      </tr>

      <tr>
        <td>City:</td>
        <td><input type="text" id="CityBox" value="" size="20" /></td>
      </tr>

      <tr>
        <td>Number Between 2-100:</td>
        <td><input type="text" id="NumberBox" value="" size="20" /></td>
      </tr>

      <tr>
        <td>Occupation:</td>
        <td><input type="text" id="OccupationBox" value="" size="20" /></td>
      </tr>

      <tr>
        <td>Hobby:</td>
        <td><input type="text" id="HobbyBox" value="" size="20" /></td>
      </tr>

      <tr>
        <td>Another City:</td>
        <td><input type="text" id="CityBox2" value="" size="20" /></td>
      </tr>
    </table>
    <hr />

    <button onclick="GenerateStory();">Click for the Story</button>
    <hr />
    <div id="outputDiv"></div>

    <script type="text/javascript">
      function GenerateStory() {
        let NameBox = document.getElementById("NameBox").value;
        let cityBox = document.getElementById("CityBox").value;
        let NumberBox = document.getElementById("NumberBox").value;
        let OccupationBox = document.getElementById("OccupationBox").value;
        let HobbyBox = document.getElementById("HobbyBox").value;
        let CityBox2 = document.getElementById("CityBox2").value;
        document.getElementById("outputDiv").innerHTML =
          "Once upon a time there was a boy named" +
          NameBox +
          "that lived in" +
          cityBox +
          "with his" +
          NumberBox +
          "siblings." +
          NameBox +
          "had a dream of being a" +
          OccupationBox +
          "so he could" +
          HobbyBox +
          "whenever he wanted and move to" +
          CityBox2 +
          "when he grew up.";
      }
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

默认情况下,原始文档是从findOneAndUpdate返回的。您需要启用new选项以获取更新的文档:

GameModel.findOneAndUpdate(req.body.filter, req.body.update, {new: true})