2017-05-03 5 views
-2

私はマングースの機能を備えたユーザ情報を更新しようと郵便配達中にこれらのエラーを得ています:userRoute.jsではTypeError:プロパティを読み取ることができません未定義のノードの「名前」

TypeError: Cannot read property 'name' of undefined 
     at router.put (/Users/wailin/Documents/Projects/Node/meanauthapp/routes/usersRoute.js:86:23) 
     at Layer.handle [as handle_request] (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/layer.js:95:5) 
     at next (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/route.js:137:13) 
     at Route.dispatch (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/route.js:112:3) 
     at Layer.handle [as handle_request] (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/layer.js:95:5) 
     at /Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:281:22 
     at param (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:354:14) 
     at param (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:365:14) 
     at Function.process_params (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:410:3) 
     at next (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:275:10) 
     at Function.handle (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:174:3) 
     at router (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:47:12) 
     at Layer.handle [as handle_request] (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/layer.js:95:5) 
     at trim_prefix (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:317:13) 
     at /Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:284:7 
     at Function.process_params (/Users/wailin/Documents/Projects/Node/meanauthapp/node_modules/express/lib/router/index.js:335:12) 

更新コードは次のとおりです。

userModel.jsで
const express = require('express'); 
const router = express.Router(); 
const passport = require('passport'); 
const jwt = require('jsonwebtoken'); 


const userModel = require('../models/usersModel'); 
const config = require('../config/_database'); 

//Update 
router.put('/update/:id', passport.authenticate('jwt', { session: false }), (request, response, next) => { 
    var userDB = userModel.getUserById(request.params.id, (err, user) => { 
     if (err) throw err; 
     else { 
      console.log('userDB : ' + user); 
     } 
    }); 

    console.log(userDB.name); 

    userModel.updateUserById(request.body.id, userDB, (err, users) => { 
     if (err) throw err; 
     else { 
      response.json({ user: users }); 
     } 
    }); 
}); 

module.exports = router; 

更新コードは次のとおりです。

const mongoose = require('mongoose'); 
const bcrypt = require('bcryptjs'); 

const config = require('../config/_database'); 

const userSchema = mongoose.Schema({ 
    name: { 
     type: String 
    }, 
    email: { 
     type: String, 
     required: true 
    }, 
    username: { 
     type: String, 
     required: true 
    }, 
    password: { 
     type: String, 
     required: true 
    } 
}); 

const User = module.exports = mongoose.model('User', userSchema); 

//GET 
module.exports.getUserById = function (id, callback) { 
    User.findById(id, callback); 
} 

//UPDATE 
module.exports.updateUserById = function (id, update, callback) { 
    const condition = { _id: id }; 
    User.findOne(condition, (err, doc) => { 
     if (err) throw err; 
     doc = update; 
     doc.save(callback); 
    }); 
} 

(PS:私は()userModel.jsにconsole.logを試し、パラメータUPDA teは 'undefined'ですが、userRoute.jsでは 'undefined'ではありません。)

+1

が重複する可能性[私は非同期呼び出しからの応答を返すにはどうしますか?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response -from-an-asynchronous-call) – peteb

答えて

0

あなたのuserDB.nameはあなたが呼び出している間は利用できません。その理由は、非同期性がJavascriptです。

この変更はうまくいくと思います。幸運..

userModel.getUserById(request.params.id, (err, user) => { 
     if (err) throw err; 
     else { 
      console.log('userDB : ' + user); 

      console.log(userDB.name); 

      userModel.updateUserById(request.body.id, userDB, (err, users) => { 
      if (err) throw err; 
       else { 
       response.json({ user: users }); 
      } 
      }); 
     } 
    }); 
+0

ありがとう、それは働いた。 –

関連する問題