PostgreSQL 模型和 MongoDB 之间的环回 HasManyThrough 产生空 GET 结果(REST API)

Loopback HasManyThrough between PostgreSQL model and MongoDB yields empty GET result (REST API)

我正在尝试在 PostgreSQL 模型(CustomerPurchase)和 MongoDB 模型(Product)之间创建一个 HasManyThrough。我已按照 https://docs.strongloop.com/display/public/LB/HasManyThrough+relations 中的指南进行操作,并设法通过启动脚本添加了一些数据(我已在 PostgreSQL 中检查记录是否正确填充)。

问题是每当我尝试使用 /Customers/{id}/products 获取模型时,我总是得到空结果(没有错误)。奇怪的是计数 API /Customers/{id}/products/count/Customers/{id}/products/{fk} 工作得很好。

任何人都可以帮助指出问题所在或建议调试问题的方法吗?

这是我的模型定义:

型号-config.json

{
  ...
  "User": {
    "dataSource": "postgresDS",
    "public": false
  },
  "Product": {
    "dataSource": "mongoDS",
    "public": true
  },
  "Customer": {
    "dataSource": "postgresDS",
    "public": true
  },
  "Purchase": {
    "dataSource": "postgresDS",
    "public": false
  },
  ...
}

customer.json

{
  "name": "Customer",
  "base": "User",
  "strict": true,
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    ...
  },
  "validations": [],
  "relations": {
    "products": {
      "type": "hasMany",
      "model": "Product",
      "foreignKey": "customerId",
      "through": "Purchase",
      "keyThrough": "productId"
    }
  },
  ...

}

purchase.json

{
  "name": "Purchase",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    ...
  },
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    },
    "product": {
      "type": "belongsTo",
      "model": "Product",
      "foreignKey": "productId"
    }
  },
  ...
}

product.json

{
  "name": "Product",
  "base": "PersistedModel",
  "idInjection": true,
  ...
  "properties": {
    ...
  },
  "relations": {
    "customers": {
      "type": "hasMany",
      "model": "Customer",
      "foreignKey": "productId",
      "through": "Purchase",
      "keyThrough": "customerId"
    }
  },
  ...
}

更新

我尝试使用 DEBUG=loopback:connector:* node . 进行调试,并在尝试调用 /Customers/{id}/products 时发现以下条目。我认为问题在于 MongoDB 查询中使用的 [Object] 。但是我不知道如何调试它。

loopback:connector:mongodb all +3ms Product { where: { id: { inq: [Object] } } }
loopback:connector:mongodb MongoDB: model=Product command=find +1ms [ { _id: { '$in': [Object] } }, [Function] ]

您需要在 Purchase 模型中定义 customerIdproductId 属性。

//purchase.json
{
  "name": "Purchase",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    ...
    "productId": {
       "type": "object"
    },
    "customerId": {
       "type": "object"
    }
  },
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    },
    "product": {
      "type": "belongsTo",
      "model": "Product",
      "foreignKey": "productId"
    }
  },
  ...
}

根据特定的数据库设置属性类型。

更新

还有product.json需要修改

//product.json
{
  "name": "Product",
  "base": "PersistedModel",
  "idInjection": true,
  ...
  "properties": {
    ...
  },
  "relations": {
    "customers": {
      "type": "hasMany",
      "model": "Customer",
      "foreignKey": "productId",
      "through": "Purchase"
    }
  },
  ...
}

我终于通过将 mongodb _id 映射到 属性 id 来实现它,如 https://github.com/strongloop/loopback-connector-mongodb/issues/52

中所述

product.json

{
    "name": "Product",
    "base": "PersistedModel",
    "idInjection": true,
    "options": {
        "validateUpsert": true
    },
    "properties": {
        "id": {
            "type": "string",
            "id": true,
            "mongodb": {
                "field": "_id"
            }
        },
        ...
    },
    "relations": {
        "customers": {
            "type": "hasMany",
            "model": "Customer",
            "foreignKey": "productId",
            "through": "Purchase",
            "keyThrough": "customerId"
        }
    },
}