【发布时间】:2018-12-08 17:02:56
【问题描述】:
VK_Accounts_schema.pre('init', function(doc) {
doc.vk = new VK(doc.access_token);
return doc;
})
VK_Accounts_schema.methods.get_API = function(cb) {
console.log(this); // i can see property vk
this.vk(...) // ERROR undefined!!!
}
我需要将 vk 准确地添加到文档中,我不需要将其保存到数据库中。
【问题讨论】:
-
究竟是什么错误? “未定义不是函数”或“vk 不是函数”?大不同。
-
TypeError: this.vk 不是函数
-
嗯,它不是一个函数,看起来像。你为什么用它作为一个?
-
我在中间件init中添加了属性vk;在 console.log(this) -> 我可以用我的函数看到属性 vk;但是 console.log(this.vk) -> undefined;
标签: javascript mongodb mongoose mongoose-schema