最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Joining two models in loopback using include filter - Stack Overflow

programmeradmin0浏览0评论

I have two models Purchase and Products, and productId is mon for both.

I need to find the productDetails from product model for a purchaseId.

So I have created a custom endpoint in Purchase model, called getProductDetails.

This is how I am trying to query the models.

         Purchase.find({
            "filter": {
                include: {
                    relation: 'Product',
                    scope: {
                        fields: ['productDesc'],
                    }
                }
            },
           where:{
                id:purchaseId
            },

My relation is Purchase belongsTo Product, foreignKey being productId

Product hasMany Purchase

But even when I do the above query, I do not get productDesc in query result

Is my model relation wrong ?

I have two models Purchase and Products, and productId is mon for both.

I need to find the productDetails from product model for a purchaseId.

So I have created a custom endpoint in Purchase model, called getProductDetails.

This is how I am trying to query the models.

         Purchase.find({
            "filter": {
                include: {
                    relation: 'Product',
                    scope: {
                        fields: ['productDesc'],
                    }
                }
            },
           where:{
                id:purchaseId
            },

My relation is Purchase belongsTo Product, foreignKey being productId

Product hasMany Purchase

But even when I do the above query, I do not get productDesc in query result

Is my model relation wrong ?

Share Improve this question edited May 13, 2016 at 12:35 Daniel Higueras 2,40423 silver badges35 bronze badges asked Apr 27, 2016 at 19:21 SantoshSantosh 1,8391 gold badge19 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Assuming Product and Purchase models defined as follows:

product.json

{
  "name": "Product",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "productDesc": {
      "type": "string",
      "required": false
    }
  },
  "validations": [],
  "relations": {
    "purchases": {
      "type": "hasMany",
      "model": "Purchase",
      "foreignKey": "productId"
    }
  },
  "acls": [],
  "methods": {}
}

purchase.json

{
  "name": "Purchase",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {},
  "validations": [],
  "relations": {
    "product": {
      "type": "belongsTo",
      "model": "Product",
      "foreignKey": "productId"
    }
  },
  "acls": [],
  "methods": {}
}

I have adjusted your query like this:

let product = await Purchase.find({
      include: {
        relation: 'product',
        scope: {
          fields: ['productDesc']
        }
      },
      where: {
        id: 1
      }
    })

Note: ES6 and ES7 used, but it can be easily rewriten to ES5

You have to use toJSON to convert the returned model instance with related items into a plain JSON object.

I'm not sure, why are you creating a new endpoint though. The ones provided by loopback should be sufficient.

e.g.

GET /Purchases/{id}/product

or

GET /Purchases/{id}

with filter

{ "include": [ "product"]}
发布评论

评论列表(0)

  1. 暂无评论