node.js - Sequelize query multiple times -


i'm trying improve search on website, how looks (i use nodejs, sequelize , postgresql):

db.food.findall({         where: {             namefood: {                 $ilike: '%' + queryname + '%'             }         }     }).then(function (foods) {         foods.sort(comparefood);          res.json(foods);     }, function (e) {         res.status(500).send();     }); 

i think pretty self explanatory, if isn't clear ask me on comments.

now, search algorithm takes consideration whole parameter, searching "chicken eggs" return nothing since on database they're saved "eggs".

my idea quick improvement split query looking spaces , query each keyword, like:

var keywords = queryname.split(' '); 

with have keywords, how can query variable number of times , join result in array returned 1 in foods?

i checked documentation , questions in here couldn't find anything, appreciated, thank much.

you can use $or property querying matching "eggs" or "chicken".

here's example:

// split queryname spaces var qry = queryname.split(' ');  // convert each item $ilike object // ['chicken', 'eggs'] -> [{ $ilike: '%chicken%' }, { $ilike: '%eggs%' }] qry = qry.map(function(item) {   return {     $ilike: '%' + item + '%';   }; });  db.food.findall({     where: {         namefood: { $or: qry }     } }).then(function (foods) {     foods.sort(comparefood);      res.json(foods); }).catch(function (e) {     res.status(500).send(); }); 

hope answers question before go i've got nice tip you might find helpful.

one of them using .catch function receiving errors. rather using .then both results , errors, can leave .then handling results , .catch handling errors.

db.food.findall()   .then(function(results) {     })    .catch(function(err) {     }); 

instead of

db.food.findall()   .then(function(results) {     }, function(err) {     }); 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -