Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 我如何更新mongodb字段,而不必为每个要更新的字段设置单独的路由?_Node.js_Mongodb_Express - Fatal编程技术网

Node.js 我如何更新mongodb字段,而不必为每个要更新的字段设置单独的路由?

Node.js 我如何更新mongodb字段,而不必为每个要更新的字段设置单独的路由?,node.js,mongodb,express,Node.js,Mongodb,Express,这是我的模式: var UserSchema = mongoose.Schema({ username: { type: String, index: true }, password: { type: String }, email: { type: String }, phonenumber: { type: Number }, fname:

这是我的模式:

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
    }
});
这是express routes,routes/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
                });
            }
        })
    });
});

是否有一种方法可以将所有这些路由放在一个路由中,并能够独立地更新单个字段?

是的,解决此问题的一种方法是在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':
            //....
或者

将更新的逻辑放在每个案例中,并确保您有一个默认值:当api没有
req.params.type
req.body.type
变量时。当然,您可以使用
if
或者if
等。。。做同样的事情,但在本例中,switch语句更显式一些


是的,解决此问题的一种方法是在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':
            //....
或者

将更新的逻辑放在每个案例中,并确保您有一个默认值:当api没有
req.params.type
req.body.type
变量时。当然,您可以使用
if
或者if
等。。。做同样的事情,但在本例中,switch语句更显式一些


它不是静止不动的。我收到一个“无法读取null的属性'update'。请查看我更新的答案,很抱歉,未经检查就剪切并粘贴了您的代码,您应该使用
.findOne()
方法查找单个用户。我现在没有错误,但它无法更新。也许我遗漏了什么,检查一下。开关(req.body.type){case'email:var email=req.body.email;用户。更新({email:email},函数(err,id){if(err){res.send(“问题更新”);}其他{res.render(“pages/profile”,{users});}}});break;有很多方法可以解决这个问题,这里有一个。你也可以通过id和update找到它,如果你有id的话,这是我建议的。它不能正常工作。我得到一个“无法读取属性'update'的null”。请查看我更新的答案,抱歉,在未检查的情况下剪切并粘贴代码,您应该使用
.findOne()
方法查找单个用户。我现在没有错误,但无法更新。可能我缺少一些内容,请检查它。开关(req.body.type){case'email':var email=req.body.email;users.update({email:email},函数(err,id){if(err){res.send(“问题更新”);}else{res.render(“页面/配置文件”,{users});}});break;有很多方法可以实现这一点,这里有一种。您还可以通过id和update查找,如果您有id,我建议您使用这一方法。