集合中的Looback mongoDB关系,仅获得ID(不是与fk相关的完整对象)

时间:2019-04-14 09:48:33

标签: javascript node.js loopbackjs strongloop

你好,我是Loopback的新手

我在这里使用回送mongodb,目前我有两个集合,即Company和Employee。

在员工集合中,我将公司称为员工表中的外键(在mongo中表示集合)。

Company.json

"properties": {
    "_id": {
      "type": "number"
    },
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
  },

Employee.json

"properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "age": {
      "type": "number",
      "required": true
    },
    "department": {
      "type": "string",
      "required": true
    },
    "city": {
      "type": "string",
      "required": true
    },
    "salary": {
      "type": "number",
      "required": true
    },
    "componyId": {
      "type": "objectid",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "compony": {
      "type": "belongsTo",
      "model": "Company",
      "foreignKey": "componyId"
    }
  },

请让我在这里遇到错误...

empoyee.js

module.exports = function(Employee) {
  console.log('Employee....:', Employee);
  Employee.findSalary = function(value, cb) {
    console.log('Employee value....:', value, cb);
    Employee.find({
      where: {
        salary: {
          gte: value,
        },
      },
      include: {
        relation: 'compony',
      },
    }, cb);
  };

  Employee.remoteMethod('findSalary', {
    accepts: {
      arg: 'salary',
      type: 'number',
    },
    returns: {
      arg: 'salarys',
      type: 'array',
    },
    http: {
      path: '/find-salary',
      verb: 'get',
    },
  });
};

谢谢

1 个答案:

答案 0 :(得分:0)

我认为您应该删除此部分:

"componyId": {
   "type": "objectid",
   "required": true
}

include: {
   relation: 'compony',
}

可能只是 include: 'compony'

而且我认为您在'comp o ny'中有一个错字,但您始终使用此名称,因此它不应只是问题而已,而不仅仅是问题