【问题标题】:"Object {} has no method 'cast' error" when trying to add item to mongoose array尝试将项目添加到猫鼬数组时,“对象 {} 没有方法 'cast' 错误”
【发布时间】:2013-03-31 11:58:00
【问题描述】:

我正在尝试使用 node.js、mongoose 和骨干创建一个 todo 应用程序以用于学习目的。 到目前为止,我定义了这些模型:

var TaskSchema = new mongoose.Schema({
    title: { type:String },
    content: { type:String } ,
    created: {type:Date, 'default':Date.now},
    due: {type:Date},
    accountId: {type:mongoose.Schema.ObjectId}
});

var Task = mongoose.model('Task',TaskSchema);

var AccountSchema = new mongoose.Schema({
    email: { type:String, unique: true},
    password: { type:String } ,
    name: { first: {type:String}, 
        last: { type:String } },
    birthday: {
        day: {type:Number, min:1, max:31, required:false},
        month: {type:Number, min:1, max:12, required:false},
        year: {type:Number}

    },
    photoUrl: {type:String},
    biography:{type:String},
    tasks:[Task]
});

var Account = mongoose.model('Account',AccountSchema);

另外,我还有如下方法添加任务

var enter_new_task = function(options,callback){
    var title = options.title;
    var content = options.content;
    var due = options.due;
    var account = options.account;
    var task = new Task({
        title: title,
        content: content,
        due: due,
        accountId: account._id
    });
    account.tasks.push(task);
    account.save(function(err) {
        if ( err ) {
            console.log("Error while saving task: " + err);
        }else{
            callback();
        }
    })
}

但是当我确实添加了一个任务时,我收到一条错误消息:

"对象 {} 没有方法 'cast'"

使用以下堆栈跟踪:

   at Array.MongooseArray._cast (/home/lior/workspace/todo_express/node_modules/mongoose/lib/types/array.js:107:30)
    at Object.map (native)
    at Array.MongooseArray.push (/home/lior/workspace/todo_express/node_modules/mongoose/lib/types/array.js:261:23)
    at Object.enter_new_task (/home/lior/workspace/todo_express/models/Account.js:107:17)
    at /home/lior/workspace/todo_express/app.js:104:18
    at Promise.<anonymous> (/home/lior/workspace/todo_express/models/Account.js:41:4)
    at Promise.<anonymous> (/home/lior/workspace/todo_express/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
    at Promise.EventEmitter.emit (events.js:95:17)
    at Promise.emit (/home/lior/workspace/todo_express/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
    at Promise.fulfill (/home/lior/workspace/todo_express/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
9

似乎问题在于新任务到任务数组的那一行。

在谷歌或堆栈上找不到任何东西,所以我想知道,有人知道出了什么问题吗?

谢谢!

【问题讨论】:

  • 不应该是tasks:[TaskSchema]而不是AccountSchema定义中的“Task”吗?
  • 你是对的......写它作为答案,我会接受它。谢谢!

标签: node.js express mongoose


【解决方案1】:

错误出现在 AccountSchema 定义中。子文档类型应该是模式,而不是模型。

var AccountSchema = new mongoose.Schema({
    //...
    tasks:[TaskSchema]
});

【讨论】:

  • 你也可以做Somemodel.schema,这是我通常做的
  • 这太对了!它肯定解决了我的问题。我很难找到这个,因为我的堆栈跟踪也有“未定义不是函数”,并且 _cast 错误有点隐藏在堆栈跟踪中。
【解决方案2】:

或者,如果您不能直接访问您的架构,而只能访问模型,您可以使用点符号访问模型的架构,如下所示:

var AccountSchema = new mongoose.Schema({
    //...
    tasks:[Task.schema]
});

如果您在另一个文件中定义了您的架构并且正在使用类似这样的内容,这将很有帮助:

module.exports = mongoose.model('Task', TaskSchema);

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 2017-12-21
    • 1970-01-01
    • 2017-12-22
    • 2020-06-24
    • 2023-03-05
    • 2019-11-30
    • 2023-03-28
    • 2021-09-08
    相关资源
    最近更新 更多