简体   繁体   English

无法读取未定义的猫鼬Node.js的属性'id'

[英]Cannot read property 'id' of undefined mongoose Nodejs

I am trying to get and remove object from mongodb. 我正在尝试从mongodb中获取删除对象。 But I am getting following error. 但是我得到以下错误。

Cannot read property 'id' of undefined 无法读取未定义的属性“ id”

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. 我想通过Schema中的Id和Id获取对象只是因为我正在尝试使用nodejs和mongoDB进行gRPC,没有数据库时gRPC代码工作正常,但是在连接到数据库后抛出错误时,当我尝试仅在nodejs中跟踪错误时遇到上述错误,最后附上测试代码。

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 这是我的tododb代码

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 注2:我也是JS及其技术的初学者

In your test code you're executing: 在测试代​​码中,您正在执行:

assert.get(40);

the assert.get execute the following function with the parameter call = 40 assert.get使用参数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 = 40,那么的值是多少

call.request.id call.request.id

call = 40 通话= 40

call.request = 40.request = undefined call.request = 40.request =未定义

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

Which give the error: 给出错误:

Cannot read property 'id' of undefined 无法读取未定义的属性“ id”


It means you are trying to access the property id on an undefined or null object 这意味着您正在尝试访问undefinednull对象的属性ID

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); 您调用assert.get(40); and the call in assert.get is number not object. assert.getcall不是数字对象。

So call=40 and call.request is undefined . 所以call=40call.requestundefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM