2016-08-02 65 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

相關問題