我正在使用REST API節點/快速應用程序。對於我的'註冊'路線,用戶使用api註冊服務,它需要POST'ed JSON對象。在這個函數裏我想檢查一下mongo db,確保這個用戶不存在。快速訪問req.body值
問題是我需要從發佈的json信息中獲取用戶名,但是我所做的每一次嘗試都失敗了。嘗試記錄req.body.username和req.body.password的行總是返回'undefined'。我究竟做錯了什麼?
這裏是我到目前爲止的代碼如下:
exports.signup = function(req, res) {
// todo: somehow verify that username, password, email and phone number are all provided.
// do not write into the collection unless we know all the information has been provided.
// maybe access the JSON elements to make sure they are not null
// todo: also make sure a record doesn't already exist for this uer
var user = req.body;
// need to get the username here somehow
var JSONuser = JSON.stringify(user);
// console.log('user: ' + user);
console.log('userJSON: ' + JSON.stringify(user));
console.log('username: ' + req.body.username);
console.log('password: ' + req.body.password);
db.collection('users', function(err, collection){
//if (collection.findOne({})) { // make sure the user doesn't already exist here
collection.insert(user, {safe:true}, function(err, result){
if(err){
res.send({'error':'An error has occured'});
} else {
console.log('Success: ' + JSON.stringify(result[0]));
res.send(result[0]);
}
})
//}
});
}
所以我在網上看了一遍,並設置了我的app.js文件來做到這一點,但它仍然在控制檯輸出中顯示爲'undefined'。這裏是我的主app.js文件的pastebin:http://pastebin.com/raw/5kSn8eLE 任何洞察爲什麼我仍然遇到問題? – user1250991
你有你的回購github,我沒有看到你的代碼有問題,我需要看到更多。 – magreenberg
當然,謝謝你看,它的github可在:https://github.com/aaronhesse/restapitest – user1250991