findOneAndUpdate并发送短信

时间:2018-10-14 08:16:47

标签: node.js express ejs

我正在尝试发送一条短信,通知患者在更新MongoDB集合约会isCheckedIn时已签入:{type:Boolean}应该设置为true。我正在使用节点js,express,ejs和MongoDB。我成功发送了一条SMS消息,但未更新。我将如何去做?

//mogoose schema
var appointmentSchema = new mongoose.Schema({
  lastName: { type: String },
  firstName: { type: String },
  phoneNumber: { type: Number },
  date: { type: String },
  time: { type: String },
  period: { type: String },
  isCheckedIn: { type: Boolean, default: false }
});
var appointment = mongoose.model("Appointment", appointmentSchema);

//SEND SMS ROUTE
app.post("/:id", function(req, res) {
  const number = ***********;
  const message = "Patient has arrived for his/her appointment";
  nexmo.message.sendSms(
    "***********",
    number,
    message,
    { type: "unicode" },
    (err, responseData) => {
      if (err) {
        console.log(err);
      } else {
        console.dir(responseData);
        res.redirect("/");
      }
    }
  );
});

// UPDATE ROUTE
app.put("/:id", function(req, res) {
  appointment.findOneAndUpdate(
    { _id: mongoose.Types.ObjectId(req.params.id) },
    { isCheckedIn: true },
    function(err, appointment) {
      if (err) {
        console.log(err);
        res.redirect("/");
      } else {
        console.log(appointment);
        res.redirect("/");
      }
    }
  );
});

解决了更新路线:

//UPDATE ROUTE
    app.put("/:id", (req, res) => {
      appointment.update(
        { _id: req.params.id },
        { $set: { isCheckedIn: true } },
        (err, responseData) => {
          if (err) {
            console.log(err);
          } else {
            console.log(responseData);
            res.redirect("/");
          }
        }
      );
    });

2 个答案:

答案 0 :(得分:0)

我认为您应该更改findOneAndUpdate。尝试以下代码段

appointment.findOneAndUpdate(
 {_id : mongoose.Types.ObjectId(req.params.id)},
 {isCheckedIn : true }
);

答案 1 :(得分:0)

//UPDATE ROUTE
    app.put("/:id", (req, res) => {
      appointment.update(
        { _id: req.params.id },
        { $set: { isCheckedIn: true } },
        (err, responseData) => {
          if (err) {
            console.log(err);
          } else {
            console.log(responseData);
            res.redirect("/");
          }
        }
      );
    });