2016-08-02 63 views
1

这里是我的架构:如何更新一个mongodb字段而不必为每个单独的字段更新单个路由?

var UserSchema = mongoose.Schema({ 
    username: { 
     type: String, 
     index: true 
    }, 
    password: { 
     type: String 
    }, 
    email: { 
     type: String 
    }, 
    phonenumber: { 
     type: Number 
    }, 
    fname: { 
     type: String 
    }, 
    lname: { 
     type: String 
    }, 
    role: { 
     type: String 
    } 
}); 

这是明确的路线,路线/ update.js。例如,在更新电子邮件或电话号码时,每条路线都会更新一个字段。

//updating email. 
app.post('/editemail', function (req, res) { 
    var username = req.body.username; 
    User.getUserByUsername(username, function (err, users) { 

     var email = req.body.email; 
     users.update({ 
      email: email 
     }, function (err, id) { 
      if (err) { 
       res.send("problem updating"); 
      } else { 
       res.render("pages/profile", { 
        users: users 
       }); 
      } 
     }) 
    }); 
}); 

//Updating phonenumber 
app.post('/editphone', function (req, res) { 
    var username = req.body.username; 
    User.getUserByUsername(username, function (err, users) { 
     var phonenumber = req.body.phonenumber; 
     users.update({ 
      phonenumber: phonenumber, 
     }, function (err, id) { 
      if (err) { 
       res.send("problem updating"); 
      } else { 
       res.render("pages/profile", { 
        users: users 
       }); 
      } 
     }) 
    }); 
}); 

有没有一种方法,我可以把所有这些路线放到一个单一的路线,并能够独立更新单个字段?

回答

0

是的,解决这个问题的一种方法是让一个变量传递给req.body,它指出你想要更新的东西。然后根据在该变量中发送的内容更新路由中的switch语句。

说你有,你从你的前端发送一个变量:{type: 'email'}

,然后在后台,你有你的路线内的switch语句:

编辑:

app.post('/edit/:type', function (req, res) { 
    var username = req.body.username; 
    User.findOne({username: username}, function (err, user) { 
     // call with POST to PATH/edit/email 
     switch(req.params.type) { 
      case 'email': 
       var email = req.body.email; 
       user.email = email; 
       user.save(function (err) { 
        if (err) res.send("problem updating"); 
        res.render("pages/profile", { 
         user: user 
        }); 
       }) 
      // call with POST to PATH/edit/phonenumber 
      case 'phonenumber': 
      //.... 

或...

app.post('/edituser', function (req, res) { 
    var username = req.body.username; 
    User.findOne({username: username}, function (err, user) { 
     // call with POST to PATH/edituser with req.body.type === 'email' 
     switch(req.body.type) { 
      case 'email': 
       var email = req.body.email; 
       user.email = email; 
       user.save(function (err) { 
        if (err) res.send("problem updating"); 
        res.render("pages/profile", { 
         user: user 
        }); 
       }) 
      // call with POST to PATH/edituser with req.body.type === 'phonenumber' 
      case 'phonenumber': 
      //.... 

在每种情况下放置更新的逻辑,并确保您有一个默认设置:用于当没有req.params.typereq.body.type变量时触发api。当然,你可以使用ifelse if等......来做同样的事情,但在这种情况下,一个switch语句更清晰一点。

You can read more about switch statements here

You can read about mongoose models here.

+0

它还没有工作。即时获取“无法读取属性'更新'null”。 – dessHub

+0

看看我更新的答案,抱歉剪切并粘贴你的代码而不检查它,你应该使用'.findOne()'方法来查找单个用户。 – alexi2

+0

我现在没有错误,但它不能更新。也许我错过了什么,检查出来。 – dessHub

相关问题