2
Category = collection.find({},{ name: true }).toArray();
console.log("Categories Found", Category);

Output:

Promise { [ 
            { name: 'Agriculture' },
            { name: 'engineer' } 
        ] }

How do I get the value of name? (NOTE: Working in node JS)

2
  • 2
    collection.find({},{ name: true }).toArray().then(function(data){ console.log("Categories Found", data); }) Commented Dec 22, 2016 at 14:22
  • Another approach would be to use the distinct() method which will give you the names in an array with a single call, as in this answer. Commented Dec 22, 2016 at 14:57

3 Answers 3

3

toArray is an asynchronous function that returns a promise. You can get your categories in one of two ways:

Promise style:

collection.find({},{ name: true }).toArray()
    .then(categories => {
        console.log(categories);
    });

Callback style:

collection.find({},{ name: true }).toArray((err, categories) => {
    console.log(categories);
});

Of course, it is a good practice to have some error handling: add .catch to the promise chain, or check if err is truthy in the callback.

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

Comments

3

Since you get back a promise, you can use .then() to get to the result, and then get to your data with a loop :)

collection
    .find({}, { name: true })
    .toArray()
    .then(function(result) { 

        result.forEach(function(data) {

            console.log("name: %s", data.name); 

        });

    });

Have fun :)

4 Comments

how do i add these names into an array. for ex: cat = ["name1","name2"]
OK I see :) you need to learn a bit more about NodeJS or rather on the new features of JavaScript. In essence you have to put your code inside the .then() and keep on nesting until you reach the end of what you want to do. This is called the Callback Hell (callbackhell.com). The name is not that nice, but this is the coding style of JavaScript. You can avoid this type of style, but that would require a lot of explanation. I give you lots of good keywords for uncle Google. You should be able to take it from hear :)
Thanks. Though I thought if there was something more optimised :)
Not sure how do you understand the word optimised but this doesn't mean is a slow approach or anything, it is just a different approach. Google also another thing NodeJS Event Loop. You'll learn why dis approach in NodeJS is the right one, and how you an make your app work very slowly if you don't understand why this is. In the last 6 months a toon of good article showed up, that will help you a lot.
0

To get the values in an array you can use the distinct() method as follows:

collection.distinct("name").then(function(categories) { 
    console.log("Categories Found", categories); 
    console.log("First Category", categories[0]); 
})

or using a callback function as:

collection.distinct("name", function(err, categories) { 
    if (err) throw err;
    console.log("Categories Found", categories); 
    console.log("First Category", categories[0]); 
})

Comments

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.