私はREST APIノード/エクスプレスアプリで作業しています。ユーザーがapiを使用してサービスにサインアップする「サインアップ」ルートでは、POSTされた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ファイルをセットアップしましたが、それでもコンソール出力に「未定義」と表示されています。私の主なapp.jsファイルのペーストビンは次のとおりです:http://pastebin.com/raw/5kSn8eLE 私がまだ問題に遭遇している理由についての洞察はありますか? – user1250991
githubにレポを持っていますが、コードに問題は見られません。もっと見る必要があります。 – magreenberg
確かに、見ていただきありがとうございます。githubはhttps://github.com/aaronhesse/restapitestにあります。 – user1250991