【问题标题】:MongoDB Put/ Update :: TypeError: {(intermediate value)} is not a functionMongoDB Put/Update :: TypeError: {(intermediate value)} 不是函数
【发布时间】:2021-07-01 21:11:38
【问题描述】:

我目前正在参加全栈 Web 开发人员训练营课程,并负责使用 CRUD 操作创建我的第一个 MERN Stack 应用程序。

更新单个项目时,我收到以下错误:

TypeError: {(intermediate value)} is not a function

我的代码如下:

  • 服务器:carsController.js:
exports.updateOneController = (req, res) => {
  Car.findByIdAndUpdate(
    req.params.id,
    { new: true }({
      $set: {
        Model: req.body.Model,
        Make: req.body.Make,
        Owner: req.body.Owner,
        Registration: req.body.registration,
        Address: req.body.Address,
        previousOwners: req.body.previousOwners,
      },
    })
  );

  Car.save()
    .then((cars) => res.json(cars))
    .catch((err) => res.status(400).json("Error updating the car." + err));
};
  • 服务器:carsRouter.js:
router.put("/updateOne/:id", cars.updateOneController);
  • 客户端:carEdit.js:
const updateOne = (e) => {
    e.preventDefault();

    axios
      .put(`cars/updateOne/${id}`, {
        method: "PUT",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          Model: Model,
          Make: Make,
          Owner: Owner,
          Registration: Registration,
          Address: Address,
          previousOwners: previousOwners,
        }),
      })
      .then((response) => {
        Swal.fire({
          imageUrl: "./images/success.gif",
          imageWidth: 150,
          imageHeight: 150,
          imageAlt: "Error",
          confirmButtonColor: "#007aff",
          width: 400,
          title: "SUCCESS!",
        });
        setCars(response.data);
      })
      .catch((error) => {
        console.log(error);
        Swal.fire({
          imageUrl: "./images/exclamation.gif",
          imageWidth: 150,
          imageHeight: 150,
          imageAlt: "Error",
          confirmButtonColor: "#ff0000",
          width: 400,
          title: "ERROR!",
          text: "User data missing",
        }).then(function () {
          window.location.reload();
        });
      });
  };
            <Button
              type="button"
              title="Update a Car"
              onClick={(e) => updateOne(e)}
            >
              <FontAwesomeIcon icon={faEdit} />
              <span id="editone">x1</span> Update
            </Button>

我不确定这是否是 findByIdAndUpdate() 和 save() 之间的冲突?

请在此处查看 GitHub 存储库的链接:https://github.com/ChanBos/MERN-Cars-Database-Application

【问题讨论】:

    标签: node.js reactjs mongodb express


    【解决方案1】:

    您在服务器代码的第 4 行缺少逗号,导致它将以下括号解释为调用操作。

    你的代码应该是:

    exports.updateOneController = (req, res) => {
      Car.findByIdAndUpdate(
        req.params.id,
        { new: true }, ({  // This is the changed line
          $set: {
            Model: req.body.Model,
            Make: req.body.Make,
            Owner: req.body.Owner,
            Registration: req.body.registration,
            Address: req.body.Address,
            previousOwners: req.body.previousOwners,
          },
        })
      );
    
      Car.save()
        .then((cars) => res.json(cars))
        .catch((err) => res.status(400).json("Error updating the car." + err));
    };
    

    我假设您没有使用常见的编辑器/IDE 之一,因为它们可以向您显示导致此错误的确切指令,从而更容易修复。程序员可能对他们的编辑器非常虔诚,但 Node.js 有许多可靠的选择。我个人使用 VS Code,但有很多不错的选择。我强烈建议找一个好的编辑器,如果你已经在使用一个,那么一定要使用他们的调试工具来追踪这些类型的错误。

    【讨论】:

    • 非常感谢您的帮助。这有助于解决错误。我也使用 VS Code。我将是第一个承认我确实需要提高调试技能的人。我的应用程序在 VS Code 中没有显示任何问题,我天真地假设一切都符合语法。我会努力提高这些技能,而不是仅仅依赖 Beautify、Prettier 或类似的东西。祝您有美好的一天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多