node.js - Mongoose Schema 上定义的实例方法的类型错误
问题描述
我在从控制器调用模型中定义的实例方法之一时收到 TypeError。
控制器:
这是我在控制器中的代码:
var Area = require('../../models/area');
Area.findOne({_id: 'Test'}).then((a) => {
a.getChildren().then((area) => {console.log(area)})
})
它返回此错误:
TypeError: a.getChildren is not a function
楷模:
这是我在模型中的代码:
areaSchema.method({
getChildren: function() {
return new Promise(function(resolve, reject) {
this.model('Area').find({path: {$regex: '/' + this.path + '/'}}).exec.then((areas) => {
return resolve(areas)
}).catch((error) => reject(error))
})
}
})
module.exports = Area;
节点控制台:
我也在节点控制台中尝试过:
var Area = require('./models/area')
var a = new Area({name: 'test', type: 'test'})
a.getChildren().then((area) => {console.log(area)})
它也返回相同的错误:
TypeError: a.getChildren is not a function
解决方案
推荐阅读
- java - KeyPressed 字母验证和 KeyEvent 组合输入限制不起作用
- python - autoninja -C out\Default chrome 给我一个错误,我不知道如何修复 windows10
- pine-script - 你能用 pine-script 来提醒你一个指标几个月来第一次超过了某个水平吗?
- database - 为什么我的 vb 代码没有更新 ms access 数据库(仅临时缓存)
- javascript - 调试动态加载的 url - 需要示例
- python - Django / 更好的方法来制作跟随模型并获得关注者和关注列表
- python - 如何拆分json数据集并保存?
- iis - IIS - 1 ApplicationPool 有 2 个站点,current_user 不同。如何改变?
- java - Is it possible to run checks on incoming HTTP requests to a spring boot application?
- javascript - 在第 1 页中为第 3 页中的 id 获取“无法读取 null 的属性‘值’”