Wednesday, 3 May 2017

TypeError: Cannot read property 'name' of undefined Node

I'm trying to update user info with mongoose function and got these errors in postman :

    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)

Update code in userRoute.js is :

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;

Update code in userModel.js is :

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);
    });
}

When I try console.log() userModel.js, the parameter update is 'undefined' while it is not 'undefined' in userRoute.js.



via Wai Lin Aung

No comments:

Post a Comment