首页 > 解决方案 > 环回查询mongodb文档

问题描述

我试图查询子文档,但它会引发以下错误。

错误:无法调用 Project.find()。尚未设置查找方法。PersistedModel 未正确附加到数据源!

我只能在资源管理器中看到项目文档,我也想看到它的子文档用户。

项目.json

{
  "name": "Project",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "user": {
      "type": "belongsTo",
      "model": "User",
      "foreignKey": "userId"
    }
  },
  "acls": [],
  "methods": {}
}

项目.js

'use strict';

module.exports = function(Project) {


Project.find().then((projects => console.log(projects))).catch((err) => console.log(err));
};

标签: javascriptnode.jsmongodbloopbackjs

解决方案


尝试将您的代码更改为:

'use strict';

module.exports = function(Project) {


Project.find().exec().then((projects => console.log(projects))).catch((err) => 
console.log(err));
};

我将 .exec() 添加到查询中。


推荐阅读