MongoDB更新问题标题-卡住

时间:2020-06-09 16:02:49

标签: node.js mongodb mongoose

我想用猫鼬更新我的问题的标题。我建立了模型并编写了路线,但由于请求未能更新我的问题的标题,我认为语法有误。 感谢您的评论。祝一切顺利

这是我的代码:

router.post("/update-titlequestion", async (req, res) => {
  try {
    const formId = req.fields.formId;

    if (formId) {
      const form = await Form.findByIdAndUpdate(formId);


      form.questions.title = req.fields.title;


      await form.save();


      return res.json(form);
    } else {
      return res.status(404).json({ error: "Form not found" });
    }
  } catch (e) {
    return res.status(400).json({ error: e.message });
  }
});

module.exports = router;

我的猫鼬模型如下:

const mongoose = require("mongoose");

const Form = mongoose.model("Form", {
  title: {
    type: String,
    required: true,
  },

  questions: [
    {
      title: {
        type: String,
      },

      answers: {
        type: Array,
      },
    },
  ],
});

module.exports = Form;

0 个答案:

没有答案