【问题标题】:How to add property to document during developing, but don't save to database?如何在开发过程中向文档添加属性,但不保存到数据库?
【发布时间】: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


【解决方案1】:
VK_Accounts_schema.methods.get_API = function(cb) {
  this.get('vk')(...) // success :)
}

【讨论】:

    猜你喜欢
    • 2016-05-09
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2016-04-08
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多