Zayn Korai
Zayn Korai

Reputation: 503

Cannot read property 'id' of undefined mongoose Nodejs

I am trying to get and remove object from mongodb. But I am getting following error.

Cannot read property 'id' of undefined

I want to get object by Id and Id in my Schema is just because I am trying out gRPC with nodejs and mongoDB, Without Database gRPC code was working fine but after connecting to database its throwing errors, when I tried to trace error only in nodejs got above mentioned error and test code is attached in last.

Note: Insert and List working fine.

Here is my mongoose schema

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const todoSchema = new Schema({
    id: {
        type: Number,
        required: true,
        unique: true,
    },
    title: {
        type: String,
        required: "Enter title"
    },
    description: {
        type: String,
        required: "Enter Description"
    },
    done: {
        type: Boolean,
        default: false
    },
    createdate: {
        type: Date,
        default: new Date()
    }
})

module.exports = mongoose.model('todo', todoSchema);

Here is my tododb code

var todoModel = require('./models/todo')

var Todo = class {

    constructor(payload) {
        this.payload = payload;
    }

    static list(callback) {
        todoModel.find({}, callback);
    }

    insert(callback) {
        new todoModel(this.payload).save(callback);
    }

    get(callback) {
        const condition = this.payload.condition;
        todoModel.find(condition). exec(callback)
    }

    delete(callback) {
        const condition = this.payload;
        todoModel.remove(condition, callback);
    }
};
module.exports = Todo;

Test Code is Here

var assert = {
    get: function (call,callback) {
        var payload = {
            condition: {
                id: call
            }
        };
        var t = new TodoDb(payload);


   t.get(callback);
    },
};


try {
    assert.get(40, callback);
    console.log('Passed.');
} catch (error) {
    console.log(error.message);
}

Note 2 : I am also beginner in JS and its tech

Upvotes: 0

Views: 1423

Answers (2)

Mumrah81
Mumrah81

Reputation: 2064

In your test code you're executing:

assert.get(40);

the assert.get execute the following function with the parameter call = 40

function (call) {
    var payload = {
        condition: {
            id: call.request.id
        }
    };
    var t = new TodoDb(payload);


    t.get(callback);
}

So if call = 40 then what is the value of

call.request.id

call = 40

call.request = 40.request = undefined

call.request.id = 40.request.id = undefined.id

Which give the error:

Cannot read property 'id' of undefined


It means you are trying to access the property id on an undefined or null object

Upvotes: 1

Here in this part of code :

var assert = {
    get: function (call) {
        var payload = {
            condition: {
                id: call.request.id
            }
        };
        var t = new TodoDb(payload);


   t.get(callback);
    },
};

You call assert.get(40); and the call in assert.get is number not object.

So call=40 and call.request is undefined.

Upvotes: 0

Related Questions