Filter By Country And Free Text Search Mongodb Using Mongoose
Solution 1:
First of all I hope you know that to perform $text
based queries you need to have a text based index on that particular field.
Secondly you seem to be confusing callbacks and promises -
As I can see your query()
function is returning a promise still when you're calling the query()
function you're expecting the callback. Promises are returned immediately to you and then you need to resolve them.
Your code should look like this -
dbHelper.query(mongoose.model('events'), {$text: {$search: "Ade"},'place.location.country': "Australia"})
.then(function(result)){
// you have your data here
}
.catch(function(err)){
// an error occured
}
Read more about Promiseshere.
Another minor bug I can notice is in the query()
method. The if statement
does not use curly brace and as a result only the very next statement after the if statement
will be executed under the if
clause. The second statement after the if
will always be executed -
So,
if(error)
console.log(error);
// always executedreject(error);
resolve(data);
should be -
if(error){
console.log(error);
reject(error);
}
resolve(data);
I'm still not sure all these would be able to make your code work as I can't see the whole picture here. I IMHO recommend that you invest some more time covering the javascript and MongoDB basics. Will help you save a lot of time. And for that matter MongoDB's official docs are really good.
Post a Comment for "Filter By Country And Free Text Search Mongodb Using Mongoose"