首页 > 解决方案 > 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;
};

标签: node.jssequelize.js

解决方案


你不能c像你的代码一样返回。我认为你应该return Project.count({ your_query })。你可以使用它Promise(我看你的代码并认为它​​是Promisethen 或者你创建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.");
    });

推荐阅读