Loopback Find()函数中的“where”语句未返回预期结果

3
我将使用loopback来为我的应用程序提供API服务,并尝试更改获取某些数据的GET请求。目前,查询会获取特定API的所有结果:
People.find({
    where: {
      'town': 'name of a town'
    }
  }).$promise
  // Promise is fulfilled and people returned
  .then(function(results) {
    $scope.people = results;
  })
  // Promise is rejected and error catched
  .catch(function(err) {
    $scope.errors.PeopleFind = JSON.stringify(err.data.error.message ?
      err.data.error.message :
      err.data.error.errmsg
    );
  }); 

我已经尝试在where子句中添加单引号或执行类似于.find({ where : { town : '城镇名称' }}的操作。

无论我在哪里放置引号,结果总是整个数据包。我该如何查询我感兴趣的结果呢?

提前致谢。


这是在前端完成的,使用已经编程好的端点和钩子。 - 404answernotfound
3个回答

11

多亏了同事,我找到了答案,我会在这里写下答案。


我通过同事找到了答案,现在我会在这里写下答案。

People
            .find({
                filter: {
                    where: {Town : currentUserTown}
                }
            })

Loopback 框架的文档没有说明您需要应用筛选对象来实际筛选结果,实际上您可以通过他们编写的以下示例检查文档:

Cars.find({where: {carClass:'fullsize'}});
在where子句对象之前,您需要编写包含子句的筛选对象,这应该可以解决查询问题。

3

使用like的方法如下:

People.find({where: {Town : like: {currentUserTown}}}, function(err, res){
//code goes here.
})

请确保您在json文件中添加了strictObjectIDCoercion

{
  "name": "People",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true,
    "strictObjectIDCoercion": true
  },
  "properties": {
 // rest of json file ...

希望这能帮到你。

谢谢Ali Bahrami。你的回答帮了我很多。 - Henok Tesfaye

0
这是一个常见的错误,请检查您相应的模型 People.json 配置文件,并将 town: {"type":"string"} 进行修改。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接