2017-03-16 75 views
0

我正在使用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]); 
      } 
     }) 
    //} 
    }); 
} 

回答

0

默認快遞,你不必通過點語法訪問這些變量。你將不得不解析迴應。幸運的是,我們有一個包裝。

使用body-parser中間件可以方便地訪問後變量。

// install it 
bash$: npm install body-parser  

// require it in your project 
bodyParser = require('body-parser'); 

// `use` it in your express app 
app.use(bodyParser.urlencoded({ extended: true})); 

// now you your post values are available on the req.body.postVariableName 

我在幾乎所有的項目中都使用了它,它只是讓它變得簡單。

*編輯*

我看着你的回購,因爲它涉及解析值的讀取一切實際上看起來罰款;然而,他們的方式你是console logging他們可能是你在哪裏得到困惑。我重寫了您的登錄路線,以便我可以更好地解釋。

exports.signin = function(req, res) { 

    var user = req.body; 
    console.log('req.body: ' + JSON.stringify(user)); 
    console.log('Signing In As User: ' + user.username); 
    console.log('Password: ' + user.password); 
    res.send('You just signed in!'); 

} 

我測試了這個,我打開了另一個終端並捲曲了JSON文章。

curl -H "Content-Type: application/json" -X POST -d '{"username":"testuser","password":"testpassword"}' http://localhost:3000/signin 

正如你可以看到它應該工作。

有些事情值得一提。當你寫console.log('req.body: ' + req.body);。你不會看到你想要的數據。您將在輸出中看到req.body: [object],因爲javascript會將其作爲僅作爲標識符的req.body.toString()呈現。如果您想發佈代碼,請使用JSON.stringify(req.body)或使用console.dir(req.body)

第二,req.body只會給你訪問的人體對象。

// this is just user.toString() which is again [object] 
console.log('Signing In As User: ' + user); 

// You need to use the dot syntax to get user.username 
console.log('Signing In As: " + user.username); 

如果你仍然在看問題,它是因爲你發佈本地主機的方式,而不是你的代碼。

+0

所以我在網上看了一遍,並設置了我的app.js文件來做到這一點,但它仍然在控制檯輸出中顯示爲'undefined'。這裏是我的主app.js文件的pastebin:http://pastebin.com/raw/5kSn8eLE 任何洞察爲什麼我仍然遇到問題? – user1250991

+0

你有你的回購github,我沒有看到你的代碼有問題,我需要看到更多。 – magreenberg

+0

當然,謝謝你看,它的github可在:https://github.com/aaronhesse/restapitest – user1250991