首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >集合中的Looback mongoDB关系,仅获取ID (不是与fk相关的完整对象)

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

Stack Overflow用户
提问于 2019-04-14 17:48:33
回答 1查看 34关注 0票数 0

你好,我是Loopback的新手

这里我使用的是loopback,mongodb,目前我有两个集合,即Company和Employee。

在employee集合中,我将公司作为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',
    },
  });
};

谢谢,

EN

回答 1

Stack Overflow用户

发布于 2019-04-15 01:51:22

我认为你应该去掉这部分:

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

include: {
   relation: 'compony',
}

可能只是include: 'compony'

我认为你在'compony‘中有一个拼写错误,但你总是使用这个名字,所以它不应该是一个问题的根源,只是一个附注

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55673929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档