Category = collection.find({},{ name: true }).toArray();
console.log("Categories Found", Category);
输出:
Promise { [
{ name: 'Agriculture' },
{ name: 'engineer' }
] }
如何获取name的值? (注意:在 Node JS 中工作)
最佳答案
toArray
是一个返回 promise 的异步函数。您可以通过以下两种方式之一获取类别:
promise 风格:
collection.find({},{ name: true }).toArray()
.then(categories => {
console.log(categories);
});
回调风格:
collection.find({},{ name: true }).toArray((err, categories) => {
console.log(categories);
});
当然,进行一些错误处理是一个很好的做法:将 .catch
添加到 promise 链中,或者在回调中检查 err
是否为真。
关于node.js - 在 Node js中获取 "name"的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41285253/