Loopback-remote方法和flatting相关模型查询结果

Loopback - remote method and flattening related model query result

我正在使用 Loopback 3 开发一个应用程序。我在 ServiceEvaluation 模型上创建了一个远程方法,用于 return ServiceEvaluations 列表和服务模型中的相关属性。

ServiceEvaluation.evaluationList = function(cb) {
    ServiceEvaluation.find({
        fields: {
            status: true,
            createdAt: true,
            serviceId: true
        },
        include: {
            relation: 'rel_ServiceEval_Service',
            scope: {
                fields: {
                    serviceName: true,
                    supplierId: true
                }
            }
        }
    }, function(err, response) {
        cb(null, response);
    });
};

当从 API Explorer returning;

调用时,以上内容有效
{
  "list": [
{
  "status": "Draft",
  "serviceId": "5b8e215d81c76325b409b960",
  "createdAt": "2018-09-04T06:08:29.623Z",
  "rel_ServiceEval_Service": {
    "serviceName": "Workplace software and SaaS",
    "id": "5b8e215d81c76325b409b960",
    "supplierId": "5b8e215d81c76325b409b949"
  }
}, ...

但是,我不想 return 包含嵌入对象的对象数组,而是 return 扁平对象数组以显示在数据网格中。以下是这样做的尝试。

ServiceEvaluation.evaluationList = function(cb) {
    ServiceEvaluation.find({
        fields: {
            status: true,
            createdAt: true,
            serviceId: true
        },
        include: {
            relation: 'rel_ServiceEval_Service',
            scope: {
                fields: {
                    serviceName: true,
                    supplierId: true
                }
            }
        }
    }, function(err, response) {
        var responseLength = response.length;
        var myEntry = {};
        var myList = [];
        for (var i = 0; i < responseLength; i++) {
            myEntry.status = response[i].status;
            myEntry.createdAt = response[i].createdAt;
            myEntry.serviceName = response[i].rel_ServiceEval_Service.serviceName;
            myEntry.supplierId = response[i].rel_ServiceEval_Service.supplierId;
            myList.push(myEntry);
        }
        cb(null, myList);
    });
};

这样做的结果是远程方法似乎找不到 rel_ServiceEval_Service 中的字段。

{
  "list": [
 {
  "status": "Draft",
  "createdAt": "2018-09-04T06:20:40.889Z"
}, ...

我已经采用扁平化客户端服务中的 return 值,但这只是开发中的临时解决方案。关于如何在远程方法中执行此操作的任何指导?

需要使用.toJSON()序列化返回数据:

ServiceEvaluation.evaluationList = function(cb) {
    ServiceEvaluation.find({
        fields: {
            status: true,
            createdAt: true,
            serviceId: true
        },
        include: {
            relation: 'rel_ServiceEval_Service',
            scope: {
                fields: {
                    serviceName: true,
                    supplierId: true
                }
            }
        }
    }, function(err, response) {
        var myEntry = {};
        var myList = [];
        async.map(response, function(singleItem,callback){
            serializedSingleItem = singleItem.toJSON()
            var myEntry = {status: serializedSingleItem.status, createdAt: serializedSingleItem.createdAt, serviceName: serializedSingleItem["rel_ServiceEval_Service"]["serviceName"], supplierId: serializedSingleItem["rel_ServiceEval_Service"]["supplierId"]}
            callback(null, myEntry)
        }, function(err, myList){
            cb(null, myList)
        })
    });
};