Mongodb findbyIdAndUpdate

时间:2018-06-25 17:37:24

标签: node.js mongodb mongoose-schema

我有点紧张,但是这段代码使我生气。 好吧,我正在尝试使用findbyIdAndUpdate将我的img src属性保存在mongodb中,但是只有输入才能正常工作。

路由JS

    //Update user profile route
router.put("/:id", middleware.isLoggedIn, function(req, res){
    User.findByIdAndUpdate(req.params.id, req.body.user,  function(err, updatedUser){
        console.log(req.body.user);
    if(err) {
        res.redirect("/:id");
        console.log(err);
    }   else {
        res.redirect("/set/"+req.params.id);
        console.log("Great!");
    }
    })
});

用户架构

var UserSchema = new mongoose.Schema({
    username: {type: String, unique: true},
    password: String,
    googleid: String,
    avatar: String,
    firstName: String,
    lastName: String,
    email: {type: String, unique: true},
    phone: Number,
    city: String,
    street: String,
    office: String,
    resetPasswordToken: String,
    resetPasswordExpires: Date
});

HTML更新表格

...
   <img id="avatar" name="user[avatar]" src="<%= user.avatar %>" alt="...">

...                  

   <label>Name</label>
  <input type="text" name="user[firstName]" class="form-control border-input" value="<%= user.firstName %>">

0 个答案:

没有答案