H5W3
当前位置:H5W3 > 其他技术问题 > 正文

mongoose下的Schema 静态方法报错is not a function?

Movie.fetch 如下:
MovieSchema.static = {

fetch: function (cb) {
    return this
        .find({})
        .sort("meta.updateAt")
        .exec(cb)
},
findById: function (id, cb) {
    return this
        .findOne({_id: id})
        .exec(cb)
}

};

入口路由文件代码如下:

app.get(“/”, function (req, res) {

Movie.fetch(function (err, movies) {
    if(err){
        console.log(err);
    }
    res.render('index',{
        'title':"imooc",
        movies: movies
    })
});

})

报错如下:

TypeError: Movie.fetch is not a function

at C:\Users\Administrator\Desktop\项目\Node\app.js:24:11
at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:271:10)
at SendStream.error (C:\Users\Administrator\Desktop\项目\Node\node_modules\serve-static\index.js:120:7)
at emitOne (events.js:77:13)
at SendStream.emit (events.js:169:7)
at SendStream.error (C:\Users\Administrator\Desktop\项目\Node\node_modules\send\index.js:245:17)
at SendStream.onStatError (C:\Users\Administrator\Desktop\项目\Node\node_modules\send\index.js:356:12)
at next (C:\Users\Administrator\Desktop\项目\Node\node_modules\send\index.js:658:28)
at C:\Users\Administrator\Desktop\项目\Node\node_modules\send\index.js:666:23
at FSReqWrap.oncomplete (fs.js:82:15)

at C:\Users\Administrator\Desktop\项目\Node\node_modules\send\index.js:666:23
at FSReqWrap.oncomplete (fs.js:82:15)

回答:

我猜测可能Scheme的静态方法需要编译成Model才能用,详见文档mongoosejs.com/docs/api.html#schema_Schema-static

回答:

schema要编译成model后才能使用,并且要用module.exports = ...导出

回答:

MovieSchema.static应该改为MovieSchema.statics

回答:

你好,请问你是怎么解决的,我按照上面评论的改,还是不能,虽然年代久远,希望回答下
,卡在这里了。

本文地址:H5W3 » mongoose下的Schema 静态方法报错is not a function?

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址