代码之家  ›  专栏  ›  技术社区  ›  Jeremy M.

在函数中返回mongoose结果

  •  1
  • Jeremy M.  · 技术社区  · 8 年前

    我正在尝试返回mongoose find操作的结果。我知道很多问题已经被问到了,但我认为这是不同的。这是我的用户:

    var UserSchema = new mongoose.Schema({
        variable: {type: mongoose.Schema.ObjectId, ref: 'Variable'}
    });
    

    我的用户有一个方法来检索他的变量。这就是问题所在。

    UserSchema.methods.getVariable = function()  {
       //TODO ?
    }
    

    我不知道如何填充我的字段,然后返回填充的结果。。。

    1 回复  |  直到 8 年前
        1
  •  0
  •   HMR    8 年前

    我想你可以用 populate

    var UserSchema = new mongoose.Schema({
      variable: {type: mongoose.Schema.ObjectId, ref: 'Variable'}
    });
    
    UserSchema.
      findOne({your:"query"}).
      populate('variable').
      exec().
      then(
        user=>console.log("user is:",user)
      );
    
    推荐文章