REST API PUT方法未提供正确的结果

 收藏

我正在学习REST API,并尝试学习CRUD方法。我正在使用MongoDB作为数据库。我正在尝试进行PUT方法并在Postman中进行了测试。但是问题是当我尝试进行放置请求并更新名称时。我可以更新的名称,但其他结果显示为空。

visulaization of null

const mongoose = require("mongoose");
const { Schema } = mongoose;

const studentModel = new Schema({
  _id: mongoose.Schema.Types.ObjectId,
  name: {
    type: String,
    required: true,
    unique: true //This key stop user to put dulicate name
  },
  birthday: {
    type: Date,
    required: true
  },
  address: {
    type: String,
    required: true,
    unique: true
  },
  zipcode: {
    type: Number,
    required: true
  },
  city: {
    type: String,
    required: true
  },
  phone: {
    type: Number,
    required: true,
    unique: true
  },

  email: {
    type: String,
    required: true,
    unique: true
  },
  course: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Course"
  }
});

const student = mongoose.model("Student", studentModel);
module.exports = student;

这是我的节点js的put方法设置

app.put("/students/:id", async (req, res, next) => {
  const id = req.params.id;

  try {
    student
      .update(
        { _id: id },
        {
          $set: {
            name: req.body.name,
            birthday: req.body.birthday,
            address: req.body.address,
            phone: req.body.phone,
            email: req.body.email
          }
        }
      )
      .then(data => {
        res.json(data);
      });
  } catch (error) {
    console.log(error);
  }
});
回复