Paul C
Paul C

Reputation: 340

Loopback - remote method and flattening related model query result

I'm developing an app using Loopback 3. I've created a remote method on on the ServiceEvaluation Model to return a list of ServiceEvaluations and related properties in the Service Model.

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);
    });
};

The above works when called from the 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"
  }
}, ...

However, rather than returning an array of objects with embedded objects, I want to return a array of flattened objects to display in a data grid. The below is an attempt to do so.

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);
    });
};

The result of this is that the remote method seems not to find the fields in the rel_ServiceEval_Service.

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

I've resorted to flattening the return value in a service on the client side but this isn only a temp solution while in Dev. Any guidance on how to do this in the remote method?

Upvotes: 0

Views: 127

Answers (1)

tashakori
tashakori

Reputation: 2441

you need to use .toJSON() to serialize the returned data:

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)
        })
    });
};

Upvotes: 1

Related Questions