FindOne ({key: value}) or findOne (). where ({key: value}) when querying a database with waterline?
I am using Waterline to query a MySQL database using Sails. I found 2 ways to do this.
I don't know which one is better?
By the way, how to handle the error for both cases?
1. Model.findOne().where({key: value}).then(function(data){
console.log(data);})
2. Phase.findOne({key: value}).then(function(data){
console.log(phase);})
+3
source to share
1 answer
Or everything will work. In this method, you will encounter an error as shown below.
1. Model.findOne().where({key: value}).then(function(data){
console.log(data);}).catch(function(err){/*....*/})
2. Phase.findOne({key: value}).then(function(data){
console.log(phase);}).catch(function(err){/*....*/})
Another variant
Phase.findOne({key: value}).exec(function(err, data){
if(err) /* Do something with error */
console.log(phase);
})
Also, if your search is using the primary key, you can do
Phase.findOne(PK)
https://github.com/balderdashy/waterline-docs/blob/master/query.md#query-language
+2
source to share