0

I have the following code in Node.js.

var getQuestionsByUserId = function (config) {
var query = {
    _id: ObjectId(String(config.userId))
};
var projection = {
    categoryIds: true,
    _id: false
};
var respondWithCategories = function (error, docs) {
    var categoryIdsList = [];    // empty array
    if (docs.length > 0) {
        console.log("Inside if: ", docs[0]);      // as expected
        categoryIdsList = docs[0].categoryIds;    // becomes undefined !! the mystery statement
    }
    console.log("Outside if: ", docs, categoryIdsList);    // as expected considering above steps
};
UserModel.find(query, projection, respondWithCategories);
};

The output is as follows

Inside if: { categoryIds: [ 5731892996a8d72df29d8554, 572f9eb779c548660331071b ] }
Outside if: [ { categoryIds: [ 5731892996a8d72df29d8554, 572f9eb779c548660331071b ] } ] undefined

I am using Mongodb with Mongoose for accessing. Mongodb version: v3.2.1
Node.js version: v5.5.0

The problem is pretty simple. How do I get the categoryIds from the object. docs is an array with single object. categoryIds is the only field in the object with a value of an array.

So I accessed by docs[0].categoryIds. I am getting undefined ! What am I missing here ?

I tried it on the browser js console and it is working fine !


Edit: (Defending the uniqueness of this question) This question, marked duplicate, has nothing to do with the asynchronous nature or callbacks even though I have mentioned the callback from mongodb find method. This question has to do with the mysterious nature (which I feel I am missing something) of the object in the array or the ObjectId interfering with the array or some way to parse the JSON object without altering the ObjectId fields returned by the mongodb.

So, please remove the duplicate marking for this question and let others help me here.


mythicalcoder
  • 3,143
  • 1
  • 32
  • 42
  • I don't know why the question is marked duplicated, as the question is completely different from the linked question. However, for the answer, try checking the type of docs[0]. It must be string. Convert it into JSON. – manish May 16 '16 at 11:53
  • This question is not duplicate of the mentioned question. @manish I cannot convert into JSON, right. This is an array that is getting passed to the callback by the mongodb find() method. So, there are ObjectId(s) in the categoryIds array. That is the reason why there are no quotes around the individual array elements on logging it. – mythicalcoder May 16 '16 at 12:18
  • Please inlcude the whole code with the `find()` method, including some sample documents to help with replicating the issue. Also mention the MongoDB and Node.js versions. – chridam May 16 '16 at 12:41
  • please add output of `console.log(docs[0])` inside the `if` statement – Alex K May 16 '16 at 14:44
  • Good request. Actually, I have that in my local code. I just didn't include it here. It is exactly the same as 1st object in the already given output. Still, sure will include it. @Alex please check now. – mythicalcoder May 16 '16 at 14:49
  • 2
    This is typically caused by `categoryIds` not being defined in the schema for `UserModel`. Can you update your question to include the schema? – JohnnyHK May 16 '16 at 14:57
  • I think, this solves my issues. I did check before, but I didn't notice it. Turns out, there was a spelling mistake in my schema. With the knowledge of your comment, I was able to spot it. Thanks. You would answer this or how can I make this resolved ? – mythicalcoder May 16 '16 at 15:04

1 Answers1

1

Thanks to @JhonnyHK. His comment helped me solve the issue.

It turned out that there was a careless mistake in the UserModel schema, spelling mistake. But good learning.

Basically, here, it means that the field is not defined in the schema. So, whenever there is a missing field in the schema, even if the object looks fine, accessing it would yield undefined in the node.js code.

Solution: Correct your schema, define the fields accessed properly in the schema. In this case, UserModel schema should be corrected.

Tip: If everything is correct from the JavaScript perspective and you have a mysterious undefined on accessing an object from mongodb find method's callback parameter, check your schema.

Community
  • 1
  • 1
mythicalcoder
  • 3,143
  • 1
  • 32
  • 42