node.js - Sequelize .count() 无法返回结果
问题描述
我正在尝试返回数字结果,但我似乎无法这样做。
如果我替换return c;
为 return 1;
我能够检索静态值。如何正确返回结果?
exports.count = function(user) {
Project.count({ where: leader: user' }).then(c => {
console.log("There are " + c + " projects with an id greater than 25.")
})
return c;
};
解决方案
你不能c
像你的代码一样返回。我认为你应该return Project.count({ your_query })
。你可以使用它Promise
(我看你的代码并认为它是Promise
)then
或者你创建exports.count
的是异步函数,你可以像你的代码一样返回。
它看起来像:
my_module.js
:
exports.count = function(user) {
return Project.count({ 'your_query' })
};
any_file.js
:
let myModule = require('./your_module');
//define user
myModule .count(user)
.then(c => {
console.log("There are " + c + " projects with an id greater than 25.");
});
推荐阅读
- java - JavaFX:java.lang.IllegalStateException:未设置位置
- django - 按存储在连接表中的值过滤 api 输出
- sql - ORACLE SQL:按天时间和金额总和分组的每小时日期
- c++ - 为什么堆栈分配的 int 数组没有显式类型 int*?
- excel - 提高宏观效率
- c# - 更新 RadDataForm 和 RadGrid 之前的确认对话框(有条件)
- android - 我应该为我的 android 应用程序图标使用什么格式?
- informix - Informix语法解释?图中的语法是什么意思?
- apache-spark - 如何生成与初始数据集最多相差一行的所有可能数据集?
- php - 如何在后台从控制器执行命令?