gpt4 book ai didi

javascript - Node 中的MongoDB聚合函数超时/不返回值

转载 作者:行者123 更新时间:2023-12-02 23:01:55 26 4
gpt4 key购买 nike

我正在尝试查询 mongoDB 集合,以便我可以返回每个 vendor 发票总额的总和,使用 mongoshell 我可以使用聚合函数工作

db.invoice.aggregate ([
{$group: {_id: "$supplier_name",
total: {$sum: "$invoice_amount"} } }
])

在我的应用程序的后端,我设置了一个路由文件夹,在这里我可以使用诸如

之类的内容成功查询我的数据库
invoiceRoute.route('/supplier-count').get((req, res) => {
Invoice.count({supplier_name: 'xyx'}, (error, data) => {
if (error) {
return next(error)
} else {
res.json(data);
}
})})

但是,当我尝试运行此代码时,我用于聚合函数的代码不会返回任何内容

invoiceRoute.route('/generate-report').get((res) => {
Invoice.aggregate ([
{$group:
{_id: "$supplier_name",
total: {$sum: "$invoice_amount"}} }
]), (error, data) => {
if (error) {
return next(error)
} else {
res.json(data);
}}});

我已经按照其他答案中的建议尝试使用 .toArray 函数,但它响应

Invoice.aggregate(...).toArray is not a function

我已经使用 postman 检查了请求,在收到响应之前它超时了,但其他请求确实返回了响应。

如有任何建议,我们将不胜感激

最佳答案

您的路由函数中缺少 reqnext:invoiceRoute.route('/generate-report').get((res) => {

invoiceRoute.route('/generate-report').get((req, res, next) => {
Invoice.aggregate([{
$group: {
_id: "$supplier_name",
total: {
$sum: "$invoice_amount"
}
}
}]), (error, data) => {
if (error) {
return next(error)
}

res.json(data);
}
});

关于javascript - Node 中的MongoDB聚合函数超时/不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57745526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com