LoopBack Remote Method返回记录数组

Geo*_*iev 5 loopback angular-loopback

我使用loopback生成我的api和AngularJS来与它通信.我有一个名为的模型Sync包含以下记录:

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}"
}
Run Code Online (Sandbox Code Playgroud)

在我的sync.js模型文件中我试图编写以下接受数字的方法(long - 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'}
 }
);
Run Code Online (Sandbox Code Playgroud)

当我在loopback explorer中尝试这个方法时,我看到这个"AssertionError"

在此输入图像描述

小智 4

您的问题一定是由于向 Sync.find() 方法提供的参数不正确造成的。(您已经为成功和错误场景提供了 2 个函数)。根据Strongloop 文档,持久模型的 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);
});
Run Code Online (Sandbox Code Playgroud)