TypeError:无法读取属性' name'未定义的节点

时间:2017-05-03 15:32:51

标签: node.js express mongoose

我尝试使用mongoose功能更新用户信息并在邮递员中收到这些错误:

    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)

userRoute.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;

userModel.js中的更新代码是:

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:当我尝试使用console.log()userModel.js时,参数更新未定义,而在userRoute.js中未定义')

1 个答案:

答案 0 :(得分: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 });
             }
           });
        }
    });