javascript - 我得到承诺使用猫鼬 find() 函数时
问题描述
我创建了一个从 MongoDB 获取所有产品列表的函数。我正在使用猫鼬包。我正在尝试控制台记录它,但相反,我得到了 Promise 。这是我的代码: -
router.get('/', function (req,res) {
//Gets all the products being sold by the particular seller
const allProducts = findAllProducts(userId);
console.log(allProducts);
})
async function findAllProducts(sellerId) {
try {
let products = await Products.find( { seller: {
Id: sellerId
}});
return products;
} catch (error) {
console.log(e);
}
}
解决方案
您需要将 async/await 移至路由功能:
router.get('/', async function (req,res) {
//Gets all the products being sold by the particular seller
const allProducts = await findAllProducts(userId);
console.log(allProducts);
})
function findAllProducts(sellerId) {
try {
return Products.find( { seller: {
Id: sellerId
}});
} catch (error) {
console.log(e);
}
}
推荐阅读
- ansible - 如何将多个远程服务器的输出保存到单个字典?
- python - 在 for 循环中创建新的变量/类实例?Python网页抓取
- apache-spark - 如何在 Pyspark 数据框中将只有字符的行与字母数字作为 ID 分开?
- android - 在KSoap2.0中添加嵌套soap属性
- php - mysqli 无法使用 ssh 隧道
- 3d - 自动去除噪点后将 PLY 转换为 STL
- google-apps-script - 存储数组以在多个触发器上使用
- centos - cifs 挂载中缺少/不同的文件
- r - 使用 which() 函数 R 两个相同的 R 日期不相等
- google-apps-script - 有没有办法在更多列中获取日期