mongoose模块中使用populate方法关联数据,报错Schema hasn’t been registered for model “…”的解决方法

别拦我!别拦我!别拦我!

我要给大家分享个bug!!!

情况是这样的,一个Node.js+MongoDB的项目中,使用了mongoose操作数据库,在使用populate()方法进行类似表关联操作的时候,报错如下:

Schema hasn't been registered for model "..."

数据库连接和业务逻辑排了了一整遍,没有问题,但只要使用populate()方法就报错。

老孙翻遍了Google上搜出来的所有相关问题的技术文档,没找到合适的方法。

由于项目已经能写了不少功能,遂单独写了个demo,测试没问题。

中间过程就不详述了,反正反复测试了N多次,最终找到了问题。

 

表关联的时候我们在Schema数据骨架中需要用ref属性定义关联集合,问题就出在这里!!!!!

这个属性的值不能有大写!必!须!小!写!!!

 

关于博主
骨灰级博客玩家
国内第一批90后网站站长/程序员
做过七年前端讲师
目前从事锦鲤观赏鱼电商行业
鱼贝贝锦鲤创始人
文章列表
1
程序员的进阶之路
程序员的进阶之路
2
koa2框架使用ctx.cookies.set()时报错ctx.cookies.set('str', encodeURIComponent('我是regcookie'))的解决方法
koa2框架使用ctx.cookies.set()时报错ctx.cookies.set('str', encodeURIComponent('我是regcookie'))的解决方法
3
换种超炫酷的姿势写代码,webstorm activate-power-mode插件安装及配置
换种超炫酷的姿势写代码,webstorm activate-power-mode插件安装及配置
4
神坑react-native之黄色警告Remote debugger is in a background tab which may cause apps to perform slowly. Fix this by foregrounding the tab (or opening it in a separate window).
神坑react-native之黄色警告Remote debugger is in a background tab which may cause apps to perform slowly. Fix this by foregrounding the tab (or opening it in a separate window).
5
webstorm设置ftp连接和自动同步的方法
webstorm设置ftp连接和自动同步的方法
最新评论
比比拉布
比比拉布
5月7日
太感谢了!!!!!!找了这么多的教程,只有你点出来了关键点——设计视图!!!!
Jake
Jake
3月7日
Halo 啊~麻烦更新下我的博客地址,原名:Jing Blog。麻烦更新如下: Jake Blog(后缀可以省略,也可以保留,看哪个风格适合) 网址:htt
评论于关于博主