0

I'm currently working on a project with mongodb/mongoose, and every time I purposely query for something that does not exist in the DB, I am getting a response with an empty array. This is my code for using Express to set up an API and return the data found in the DB:

app.get('/api/:id', function(req, res) {
var id = req.params.id;
Job.find({jobID: id}, function (err, foundJob) {
    if (err) {
        console.log(err);
    }
    else {
        res.json(foundJob);
    }
  });
});

However, every time I go to localhost:3000/api/67 (I have no object with jobID: 67 in the database yet), the console does not print the error. It gives me a JSON response with an empty array. Any ideas to why this is happening? The weird part is that when I change jobID: id to _id: id, it does give me an error. Why doesn't it do that for the jobID field?

EDIT: Just to clarify, the reason why I do not want this behavior is because my program will never print the error, even if a job in my DB doesn't exist with that specified jobID.

0

3 Answers 3

1

It does something different than you think it does.

Job.find({jobID: id}, ... )

What this really says is give me array with all the documents in collection "Job" that have field "jobID" equal to some value.

What if there is no such document? Well, then the array will be empty. Without any error, of course, what should be an error here? You asked for all documents (given some filter) and an array with all such documents was returned. It is just a coincidence that the size of the array is zero, because there are no such documents.

If you want to check whether there is no such document then check whether the array is empty.

I don't know why it is giving you error when you change JobID to _id; what error exactly is it?

If you are interested only in one document, then there is method findOne that returns only the first document (or null if no such documents exist) instead of an array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Your explanation helped me understand it more, and I then added this conditional statement to fix it: else if (!res.length){ console.log("Nothing found"); }
0

About error when you try to find something by it's __id: It gives you a error because __id is not String, it's ObjectId. So you should search for document with that __id like this: _id: ObjectId(id)


About empty string: If you want to display some kind of different message you should check if db returned something or is it rather empty string that got returned. Something like this:

app.get('/api/:id', function(req, res) {
var id = req.params.id;
Job.find({jobID: id}, function (err, foundJob) {
    if(foundJob){
       res.json(foundJob);
    }else{
       res.json("nothing found");
    }
if (err) {
        console.log(err);
    }
  });
});

Edit: I didnt realize that you had check for error, I changed code.

2 Comments

That yielded the same results as before :/
@calviners I edited it for third time. This should work 100%.
0

Returning an empty string is normal behavior for mongoose. You should handle your response like this:

if (err) {
    //handle error
} else if (foundJob) {
    //work with your data
} else {
    //nothing was found
}

The error you get with _id must be irrelevant and is probably due to an invalid query.

1 Comment

By saying same results what exactly do you mean? Are you getting into the else if(foundJob){} even when your array is empty?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.