if id not receve from query i have to fetch the whole data in the database other wise if i receive id the i have to find the specific data from collection according to id
getCandidate(language,req,callback)
{
this.Trainer.find({},function(err,data){
if (err) {
callback(err);
}
if (data && data.length === 0) {
return callback(false, common.errorMsg("NO STATE FOUND"));
} else {
return callback(false, common.successMsg("", data));
}
})
}
You are going to have to provide some more information and context here.
What is the DB and which ORM/ODM if any are you using to make DB queries?
What is the server set up, is it Express?
If the id is not found (on the req) use whatever method that gives you everything if there is an id use that for the DB query. The queries are both DB and ORM/ODM specific.
ive have data of candidates saved in trainer collection ,there is a two condition
at 1st if i send id then i have to show the data who matches the id and if i dont send the id then its should return the whole data stored in the collection
like if i run find query without any conditions iam avoiding to use if else condition
as i told you i know how to use find query in mongoose but there is conditions here 1.id is not getting from query to print whole data from data base 2 . sending id to show a specific data from database with matching id
Create a condition where you check the req for an id and do one thing otherwise do something else. How you do it is up to you but I’m sure you must have coded something similar before.
One option might be to construct the query object using the conditions before making the query. So it will be an empty object if you do not get an id or an object with that id set to the _id property.
This sounds like an assignment. If so whatever you learned beforehand should have covered the prerequisite knowledge to solve this task. Or you either skipped something in the process or maybe it isn’t a very good learning resource.
If you have absolutely no idea how to approach this problem I’d suggest doing the freeCodeCamp JS curriculum. Just to get more of the basics in place first.
After that, you can do the backend part of the curriculum