按计算属性搜索Sails.js
我正在sails.js中编写API,在查找对象方面有一个问题 我有一个具有以下属性的模型:按计算属性搜索Sails.js,sails.js,Sails.js,我正在sails.js中编写API,在查找对象方面有一个问题 我有一个具有以下属性的模型: attributes: { firstName:{ type:"string", required:true, minLength: 2 }, lastName:{ type:"string", required:true, minLength: 2 }, getFullName: fu
attributes: {
firstName:{
type:"string",
required:true,
minLength: 2
},
lastName:{
type:"string",
required:true,
minLength: 2
},
getFullName: function (){
var fl = this.firstName + " " + this.lastName;
return fl;
},
}
现在我想找到一个getFullName startsWith“xyz qwe”的对象
我怎么做
我试过:
Patient.find({getFullName:{'startsWith':'Tomas'}).exec(console.log)
及
Patient.find({getFullName():{'startsWith':'Tomas'}).exec(console.log)
而且两者都不起作用
我可以在find()函数中访问诸如getFullName之类的计算属性吗
当然,这个查询是有效的:
Patient.find({firstName:{'startsWith':'Tomas'}).exec(console.log)
exec
或回调有2个参数(与任何其他回调一样),它是错误和结果。所以你的代码应该是这样的
Patient
.find({ getFullName: { 'startsWith': 'Tomas' }})
.exec(function(err, founds){
if(err) return console.error(err);
console.log(founds);
});
或者你可以使用承诺链来做你以前做过的事情
Patient
.find({ getFullName: { 'startsWith': 'Tomas' }})
.then(console.log)
.catch(console.error);
exec
或callback有两个参数(与任何其他回调一样),它是错误
和结果
。所以你的代码应该是这样的
Patient
.find({ getFullName: { 'startsWith': 'Tomas' }})
.exec(function(err, founds){
if(err) return console.error(err);
console.log(founds);
});
或者你可以使用承诺链来做你以前做过的事情
Patient
.find({ getFullName: { 'startsWith': 'Tomas' }})
.then(console.log)
.catch(console.error);
这不是问题:.exec(console.log)
工作正常。问题是:如何在find()方法中使用computed属性(实例函数)。顺便说一句,我正在sails控制台中测试它
,这就是为什么我使用.exec(console.log)
对不起,我错了,我误解了你的问题。当我有时间的时候,我会编辑我的答案,如果它仍然没有解决。我已经通过复杂的查找查询解决了它。但是我的问题比我的问题更复杂,请告诉我你解决这个问题的方法。我希望它比我的好。这不是问题:.exec(console.log)
工作正常。问题是:如何在find()方法中使用computed属性(实例函数)。顺便说一句,我正在sails控制台中测试它
,这就是为什么我使用.exec(console.log)
对不起,我错了,我误解了你的问题。当我有时间的时候,我会编辑我的答案,如果它仍然没有解决。我已经通过复杂的查找查询解决了它。但是我的问题比我的问题更复杂,请告诉我你解决这个问题的方法。我希望它比我的好。