countDocuments() 在 api 调用中不起作用
countDocuments() is not working in api call
我正在尝试使用 api 调用来获取产品数量,但在邮递员中它一直在加载
router.get(`/get/count`, async (req, res) => {
const productCount = await Product.countDocuments((count)=>count)
if (!productCount) {
res.status(500).json({ success: false });
}
res.send({
productCount: productCount
});
});
(node:28030) UnhandledPromiseRejectionWarning: MongooseError: Query was already executed: Product.countDocuments({})
没有 async 和 await 也不起作用
我尝试捕获错误,但我在邮递员中收到了这个错误
{
"success": false,
"error": "Query was already executed: Product.countDocuments({})"
}
捕获错误的代码:
router.get(`/get/count`, (req, res) => {
Product.countDocuments((count)=>count).then((pcount)=>{
if(pcount){
return res.status(200).json({success:true})
}else{
return res.status(404).json({success:false})
}
}).catch((err)=>{
return res.status(400).json({success:false, error:err.message})
})
});
我认为 在 Mongoose 操作中你想要或者 await
或者提供回调,但不能同时提供两者。尝试同时执行这两项操作会导致它在内部执行查询两次。
试试看:
const productCount = await Product.countDocuments();
如果您想计算产品集合中的所有产品,试试这个
db.product.countDocuments({})
router.get(`/get/count`, async(req, res) => {
let productCount = await Product.countDocuments();
if(!productCount){
res.send(500).json({success:false})
}
res.send({productCount: productCount})
});
有两种方法可以做到这一点
Try it you don't need use callback (count)=>count
const express = require('express')
const router = express.Router();
const {Product} = require('../models/products')
const {Category} = require('../models/category')
const catchAsyncErrors = require('../middleware/catchAsyncError')
const mongoose = require('mongoose');
// Get all product
router.get(`/`, async (req, res) => {
let productList = await Product.find().populate('category')
if(!productList) {
res.status(500).json({
success: false
})
}
res.status(200).json({
success: true,
count: productList.length,
productList
});
})
router.get(`/count`, catchAsyncErrors(async(req, res) => {
const countProductList = await Product.countDocuments();
if(!countProductList){
res.status(500).json({
success: false
})
}
res.send({
success: true,
countProduct: countProductList
})
}))
您不需要包含 ((count)=>count)
。
使用 Product.countDocuments()
代替
我正在尝试使用 api 调用来获取产品数量,但在邮递员中它一直在加载
router.get(`/get/count`, async (req, res) => {
const productCount = await Product.countDocuments((count)=>count)
if (!productCount) {
res.status(500).json({ success: false });
}
res.send({
productCount: productCount
});
});
(node:28030) UnhandledPromiseRejectionWarning: MongooseError: Query was already executed: Product.countDocuments({})
没有 async 和 await 也不起作用
我尝试捕获错误,但我在邮递员中收到了这个错误
{
"success": false,
"error": "Query was already executed: Product.countDocuments({})"
}
捕获错误的代码:
router.get(`/get/count`, (req, res) => {
Product.countDocuments((count)=>count).then((pcount)=>{
if(pcount){
return res.status(200).json({success:true})
}else{
return res.status(404).json({success:false})
}
}).catch((err)=>{
return res.status(400).json({success:false, error:err.message})
})
});
我认为 在 Mongoose 操作中你想要或者 await
或者提供回调,但不能同时提供两者。尝试同时执行这两项操作会导致它在内部执行查询两次。
试试看:
const productCount = await Product.countDocuments();
如果您想计算产品集合中的所有产品,试试这个
db.product.countDocuments({})
router.get(`/get/count`, async(req, res) => {
let productCount = await Product.countDocuments();
if(!productCount){
res.send(500).json({success:false})
}
res.send({productCount: productCount})
});
有两种方法可以做到这一点
Try it you don't need use callback (count)=>count
const express = require('express')
const router = express.Router();
const {Product} = require('../models/products')
const {Category} = require('../models/category')
const catchAsyncErrors = require('../middleware/catchAsyncError')
const mongoose = require('mongoose');
// Get all product
router.get(`/`, async (req, res) => {
let productList = await Product.find().populate('category')
if(!productList) {
res.status(500).json({
success: false
})
}
res.status(200).json({
success: true,
count: productList.length,
productList
});
})
router.get(`/count`, catchAsyncErrors(async(req, res) => {
const countProductList = await Product.countDocuments();
if(!countProductList){
res.status(500).json({
success: false
})
}
res.send({
success: true,
countProduct: countProductList
})
}))
您不需要包含 ((count)=>count)
。
使用 Product.countDocuments()
代替