だから、これは何時間も苦労していて、それを動作させる方法を見つけることができません。ノードjs + Express + Passport + MongoDBのユーザレコードを更新する
私が急行し、パスポート、MongoDBのでnodejsを設定する上で完了するまで、このチュートリアルに従っ: https://scotch.io/tutorials/easy-node-authentication-setup-and-local それが今、私は、ユーザーがフォームを介して、表示名を作成できるように認証されたセッションを使用したい除いて、素晴らしい作品。
route/passport/sessionメソッドを使ってmongodbのユーザーレコードをPOSTリクエストで更新する方法がわからないので、私は困惑しています。
私の質問は、以下のコードの 'Process Update Profile'ブロックに関連しています。 user.update関数を使用して、ユーザー名レコードにdisplaynameを追加しようとしています。私は次のエラーを取得する:
ReferenceError: user is not defined
at Object.handle (/vagrant/new/app/routes.js:65:9)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:103:13)
at Object.isLoggedIn [as handle] (/vagrant/new/app/routes.js:115:16)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/vagrant/new/node_modules/express/lib/router/route.js:107:5)
at /vagrant/new/node_modules/express/lib/router/index.js:195:24
at Function.proto.process_params (/vagrant/new/node_modules/express/lib/router/index.js:251:12)
at next (/vagrant/new/node_modules/express/lib/router/index.js:189:19)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:77:14)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:81:14)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:81:14)
at Route.dispatch (/vagrant/new/node_modules/express/lib/router/route.js:107:5)
at /vagrant/new/node_modules/express/lib/router/index.js:195:24
at Function.proto.process_params (/vagrant/new/node_modules/express/lib/router/index.js:251:12)
at next (/vagrant/new/node_modules/express/lib/router/index.js:189:19)
at next (/vagrant/new/node_modules/express/lib/router/index.js:166:38)
私は、ユーザーが私が使用している関数に渡されていないことを確認することができますが、私は正しい方法がどうあるべきかを把握することはできません - あるいは、私が近づいていた場合これはまったく正しい方法です。 下記のコードを参照してください。
// app/routes.js
module.exports = function(app, passport) {
// =====================================
// HOME PAGE (with login links) ========
// =====================================
app.get('/', function(req, res) {
res.render('index.ejs'); // load the index.ejs file
});
//... some code (login, signup methods) omitted for brevity ...
// =====================================
// UPDATE PROFILE =================
// =====================================
app.get('/updateprofile', isLoggedIn, function(req, res) {
res.render('updateprofile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
// =====================================
// PROCESS UPDATE PROFILE=======================
// =====================================
// process the update profile form
app.post('/updateprofile', isLoggedIn, function(req, res) {
user.update({_id: req.session.passport.user}, {
displayName: req.body.displayName
}, function(err, numberAffected, rawResponse) {
console.log('new profile update error');
});
res.render('profile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
// =====================================
// PROFILE SECTION =====================
// =====================================
// we will want this protected so you have to be logged in to visit
// we will use route middleware to verify this (the isLoggedIn function)
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
}
var user = require('../app/models/user'); //i get the address of user model in the link you give, but in general it should be the user model address.
あなたが使用しているため、更新:あなたはあなたのroutes.jsファイルの先頭に次の行を追加する必要があります
ありがとう、それはエラーを修正 - 私のデータベースはまだ表示名の新しい値で更新されません。 mongodbの更新を呼び出すupdate関数か、今作成したばかりのユーザーインスタンスですか? – user1871200
はまた、ここに/アプリ/モデル/ユーザーでの私のスキーマです: するvar userSchema = mongoose.Schema({ ローカル:{ メール:文字列、 パスワード:文字列、 のdisplayName:文字列 }、 グーグル:{ ID:文字列、 トークン:文字列、 メール:文字列、 名:文字列、 のdisplayName:文字列 }})。 – user1871200
@ user1871200、私はあなたの答えを更新しました。 – Pourya8386