2016-06-22 39 views
4

我用loopback生成我的api和AngularJS来与它通信。我有一个名为Sync模型,包含以下记录:LoopBack远程方法返回记录数组

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}" 
} 
我sync.js型号文件

我想写以下方法接受号(长 - 在timeChanged),并应返回的所有记录即具有相等或相等的timeChanged字段。

这是我在这里:

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

当我尝试在回送探险这个方法我看到这个“Asse田”

enter image description here

回答

2

你的问题一定是由于不正确的参数提供给Sync.find()方法。 (您已为成功和错误情况提供了2个功能)。根据Strongloop documentation,持久化模型的find函数有2个参数即。一个可选的过滤器对象和一个回调函数。回调使用节点错误优先风格。

请尝试更改您Sync.find(),以类似下面:

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

谢谢!成功了! :) –