PostgreSQL模型和MongoDB之间的环回有很多漏洞,导致GET结果为空(RESTAPI)

PostgreSQL模型和MongoDB之间的环回有很多漏洞,导致GET结果为空(RESTAPI),mongodb,postgresql,loopbackjs,Mongodb,Postgresql,Loopbackjs,我试图在PostgreSQL模型(Customer,Purchase)和MongoDB模型(Product)之间建立一个HasManyThrough)。我按照中的指南,通过引导脚本添加了一些数据(我在PostgreSQL中检查了记录是否正确填充) 问题是每当我尝试使用/Customers/{id}/products获取模型时,总是得到空结果(没有错误)。奇怪的是,countapi/Customers/{id}/products/count和/Customers/{id}/products/{fk

我试图在PostgreSQL模型(
Customer
Purchase
)和MongoDB模型(
Product
)之间建立一个
HasManyThrough
)。我按照中的指南,通过引导脚本添加了一些数据(我在PostgreSQL中检查了记录是否正确填充)

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

有没有人能帮我指出问题所在或建议调试问题的方法

以下是我的模型定义:

model config.json

{
  ...
  "User": {
    "dataSource": "postgresDS",
    "public": false
  },
  "Product": {
    "dataSource": "mongoDS",
    "public": true
  },
  "Customer": {
    "dataSource": "postgresDS",
    "public": true
  },
  "Purchase": {
    "dataSource": "postgresDS",
    "public": false
  },
  ...
}
{
  "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"
    }
  },
  ...
{
  "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"
    }
  },
  ...
}
{
  "name": "Product",
  "base": "PersistedModel",
  "idInjection": true,
  ...
  "properties": {
    ...
  },
  "relations": {
    "customers": {
      "type": "hasMany",
      "model": "Customer",
      "foreignKey": "productId",
      "through": "Purchase",
      "keyThrough": "customerId"
    }
  },
  ...
}
{
    "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"
        }
    },
}
customer.json

{
  ...
  "User": {
    "dataSource": "postgresDS",
    "public": false
  },
  "Product": {
    "dataSource": "mongoDS",
    "public": true
  },
  "Customer": {
    "dataSource": "postgresDS",
    "public": true
  },
  "Purchase": {
    "dataSource": "postgresDS",
    "public": false
  },
  ...
}
{
  "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"
    }
  },
  ...
{
  "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"
    }
  },
  ...
}
{
  "name": "Product",
  "base": "PersistedModel",
  "idInjection": true,
  ...
  "properties": {
    ...
  },
  "relations": {
    "customers": {
      "type": "hasMany",
      "model": "Customer",
      "foreignKey": "productId",
      "through": "Purchase",
      "keyThrough": "customerId"
    }
  },
  ...
}
{
    "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"
        }
    },
}
}

purchase.json

{
  ...
  "User": {
    "dataSource": "postgresDS",
    "public": false
  },
  "Product": {
    "dataSource": "mongoDS",
    "public": true
  },
  "Customer": {
    "dataSource": "postgresDS",
    "public": true
  },
  "Purchase": {
    "dataSource": "postgresDS",
    "public": false
  },
  ...
}
{
  "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"
    }
  },
  ...
{
  "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"
    }
  },
  ...
}
{
  "name": "Product",
  "base": "PersistedModel",
  "idInjection": true,
  ...
  "properties": {
    ...
  },
  "relations": {
    "customers": {
      "type": "hasMany",
      "model": "Customer",
      "foreignKey": "productId",
      "through": "Purchase",
      "keyThrough": "customerId"
    }
  },
  ...
}
{
    "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"
        }
    },
}
product.json

{
  ...
  "User": {
    "dataSource": "postgresDS",
    "public": false
  },
  "Product": {
    "dataSource": "mongoDS",
    "public": true
  },
  "Customer": {
    "dataSource": "postgresDS",
    "public": true
  },
  "Purchase": {
    "dataSource": "postgresDS",
    "public": false
  },
  ...
}
{
  "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"
    }
  },
  ...
{
  "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"
    }
  },
  ...
}
{
  "name": "Product",
  "base": "PersistedModel",
  "idInjection": true,
  ...
  "properties": {
    ...
  },
  "relations": {
    "customers": {
      "type": "hasMany",
      "model": "Customer",
      "foreignKey": "productId",
      "through": "Purchase",
      "keyThrough": "customerId"
    }
  },
  ...
}
{
    "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"
        }
    },
}
更新

我尝试使用
debug=loopback:connector:*节点进行调试。
并在尝试调用
/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
模型中定义
customerId
productId
属性

//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
\u id
映射到属性
id
,如中所述

product.json

{
  ...
  "User": {
    "dataSource": "postgresDS",
    "public": false
  },
  "Product": {
    "dataSource": "mongoDS",
    "public": true
  },
  "Customer": {
    "dataSource": "postgresDS",
    "public": true
  },
  "Purchase": {
    "dataSource": "postgresDS",
    "public": false
  },
  ...
}
{
  "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"
    }
  },
  ...
{
  "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"
    }
  },
  ...
}
{
  "name": "Product",
  "base": "PersistedModel",
  "idInjection": true,
  ...
  "properties": {
    ...
  },
  "relations": {
    "customers": {
      "type": "hasMany",
      "model": "Customer",
      "foreignKey": "productId",
      "through": "Purchase",
      "keyThrough": "customerId"
    }
  },
  ...
}
{
    "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"
        }
    },
}

谢谢你的回复!不幸的是,它仍然不起作用。我已经尝试对类型使用
对象
字符串
,但结果仍然为空。你有任何有效的例子吗?我已经按照你的建议更新了我的模型定义(也反映在我的问题中),但不幸的是仍然没有working@YohanesGultom我仍然认为
产品
@YohanesGultom
产品
的定义是错误的,定义应该与
客户
相同,但反过来说,我的错误,这是一个打字错误(已修复)。我已经照你的建议做了,但不幸的是还是一样