node.js - Sequelize .count() 无法返回结果

标签 node.js sequelize.js

我试图返回数字结果,但我似乎无法这样做。

如果我用 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.");
    });

关于node.js - Sequelize .count() 无法返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51116299/

相关文章:

javascript - 为什么我的代码可以在标准 Node.js 文件中运行,而不能在 AWS Lambda 函数中运行?

node.js - Mongoose .save 不是一个函数

javascript - 如何连续运行 mocha 集成测试?

sql-server - 在 SQL Server 中插入时根据 ID 计算的列值

javascript - sequelize 模型中字段的数据类型

node.js - Sequelize 批量更新数组问题

node.js - NodeJS 在 Docker 上安装

javascript - Webpack 在目标 Node 时忽略代码拆分

mysql - SequelizeEagerLoadingError : associated not working in sequelize

node.js - 生成 Joi 验证 Sequelize 模型