Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoopBack Remote Method to return array of records

I used loopback to generate my api and AngularJS to communicate with it. I have a model called Sync that contains the following records:

Sync": {
"34": "{\"uuid\":\"287c6625-4a95-4e11-847e-ad13e98c75a2\",\"table\":\"Property\",\"action\":\"create\",\"timeChanged\":1466598611995,\"id\":34}",
"35": "{\"uuid\":\"287c6625-4a95-4e11-847e-ad13e98c75a2\",\"table\":\"Property\",\"action\":\"update\",\"timeChanged\":1466598625506,\"id\":35}",
"36": "{\"uuid\":\"176aa537-d000-496a-895c-315f608ce494\",\"table\":\"Property\",\"action\":\"update\",\"timeChanged\":1466598649119,\"id\":36}"
}

in my sync.js Model file I am trying to write the following method that accepts number (long - the timeChanged) and should return all of the records that are have equal or equal timeChanged field.

This is where I am at:

Sync.getRecodsAfterTimestamp = function(timestamp, cb){
var response = [];
Sync.find(
  function(list) {
    /* success */
  // DELETE ALL OF THE User Propery ratings associated with this property
  for(i = 0; i < list.length; i++){
    if(list[i].timeChanged == timestamp){
      response += list[i];
      console.log("Sync with id: " + list[i].id);
    }
  }
  cb(null, response);
},
function(errorResponse) { /* error */ });
}

Sync.remoteMethod (
'getRecodsAfterTimestamp',
{
  http: {path: '/getRecodsAfterTimestamp', verb: 'get'},
  accepts: {arg: 'timeChanged', type: 'number', http: { source: 'query' } },
  returns: {arg: 'name', type: 'Array'}
 }
);

When I try this method in the loopback explorer I see this "AssertionError"

enter image description here

like image 777
Georgi Koemdzhiev Avatar asked Jun 22 '16 14:06

Georgi Koemdzhiev


People also ask

How do I register a remote method in loopback?

There are two ways to register a remote method: In code, in the model extension file ( modelName.js ). In JSON, in the model definition JSON file ( modelName.json ). LoopBack models have a remoteMethod () static method that you use to register a remote method:

When should I migrate from loopback 3 to loopback 4?

We urge all LoopBack 3 users to migrate their applications to LoopBack 4 as soon as possible. Learn more about LoopBack's long term support policy. A remote method is a static method of a model, exposed over a custom REST endpoint. A remote method is a method of a model, exposed over a custom REST endpoint.

How do I return an array from a remote method?

If you want to return data as the main response, for example an array, you can do so by setting the rootproperty within the returns object and omitting arg. returns:{type:'array',root:true} Setting a remote method route By default, a remote method is exposed at: POST http://apiRoot/modelName/methodName Where

How does loopback determine the value of an input parameter?

If you don’t specify a mapping, LoopBack will determine the value as follows (assuming name as the name of the input parameter to resolve): If there is an HTTP request parameter args with JSON content, then it uses the value of args['name']. Otherwise, it uses req.param('name'). Returning data outside of a JSON field


1 Answers

Your problem must be due to incorrect arguments supplied to the Sync.find() method. (You have provided 2 functions for success and error scenarios). As per the Strongloop documentation, the persisted model's find function has 2 arguments viz. an optional filter object and a callback. The callback uses the node error-first style.

Please try changing your Sync.find() to something like below:

Sync.find(function(err, list) {
if (err){
    //error callback
}
    /* success */
// DELETE ALL OF THE User Propery ratings associated with this property
for(i = 0; i < list.length; i++){
    if(list[i].timeChanged == timestamp){
        response += list[i];
        console.log("Sync with id: " + list[i].id);
    }
}
cb(null, response);
});
like image 136
Arun N. Kutty Avatar answered Oct 20 '22 14:10

Arun N. Kutty