【问题标题】:Get many results in Sequelize在 Sequelize 中获得许多结果
【发布时间】:2015-12-16 20:23:32
【问题描述】:

如何在 Sequelize in array 中获得多个结果?示例:我需要在表test 中获取所有值字段name 并在控制台中返回它。我写:

test.findAll().them(function(result) {
    result.forEach(function(item) {
        console.log(item.name);
    });
});

如何在没有forEach()的情况下获取数组中的所有值字段name

(抱歉英语不好)

【问题讨论】:

  • 您需要搜索所有结果和投影列name 还是按name 过滤?

标签: javascript node.js sequelize.js


【解决方案1】:

您可以使用map 将名称提取到数组中。

test.findAll().then(function(result) {
    var names = result.map(function(item) {
        return item.name;
    });
    console.log(names);
});

如果您担心数据库返回您不关心的其他字段,您可以使用findAllattributes 选项,正如DevAlien 所述:

test.findAll( {attributes: ['name']} ).then(function(result) {
    var names = result.map(function(item) {
        return item.name;
    });
    console.log(names);
});

【讨论】:

    【解决方案2】:
    test.findAll({attributes: ['name']}).them(function(result) {
        console.log(result);
    });
    

    【讨论】:

    • 不,都是结果。我只需要值字段name
    猜你喜欢
    • 2021-06-13
    • 2023-02-06
    • 2020-08-22
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多