loopback Find() "where" 子句未返回预期结果
loopback Find() "where" clause not returning expected results
我正在使用环回为我的应用程序提供 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 : 'name of a town' }}
的事情。
无论我把引号放在哪里,结果总是完整的。我如何查询我感兴趣的结果?
提前致谢
感谢同事找到答案,我把答案写在这里
People
.find({
filter: {
where: {Town : currentUserTown}
}
})
环回框架的文档没有说明您需要应用过滤器对象来实际过滤结果,实际上您可以使用他们写的这个示例查看文档:
Cars.find({where: {carClass:'fullsize'}});
在where子句对象之前需要写包含该子句的过滤对象,这样应该可以解决查询的问题。
像这样使用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 ...
希望这对您有所帮助。
这是常见的错误,请检查您各自的型号 People.json 配置文件,并使 town: {"type":"string"}
我正在使用环回为我的应用程序提供 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 : 'name of a town' }}
的事情。
无论我把引号放在哪里,结果总是完整的。我如何查询我感兴趣的结果?
提前致谢
感谢同事找到答案,我把答案写在这里
People .find({ filter: { where: {Town : currentUserTown} } })
环回框架的文档没有说明您需要应用过滤器对象来实际过滤结果,实际上您可以使用他们写的这个示例查看文档:
Cars.find({where: {carClass:'fullsize'}});
在where子句对象之前需要写包含该子句的过滤对象,这样应该可以解决查询的问题。
像这样使用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 ...
希望这对您有所帮助。
这是常见的错误,请检查您各自的型号 People.json 配置文件,并使 town: {"type":"string"}