簡體   English   中英

集合中的looback mongoDB關系,僅獲取ID(不是與fk相關的完整對象)

[英]Looback mongoDB relation in collection, get in ID only (not complete object related to fk)

你好,我是Loopback的新手

在這里我使用的是loopback,mongodb,目前我有兩個與我合作的集合,即公司和員工。

在員工集合中,我將公司稱為員工表中的外鍵(表示在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',
    },
  });
};

謝謝,

我想你應該刪除這部分:

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

include: {
   relation: 'compony',
}

可能只是include: 'compony'

而且我認為你在'comp o ny'中有一個拼寫錯誤,但是你一直使用這個名字所以它不應該是問題的根源而只是一個旁注

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM