gpt4 book ai didi

node.js - 使用 Sequelizejs 检索多行

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:08 25 4
gpt4 key购买 nike

我开始学习 SequelizeJs 但遇到一个小问题:我有一个定义如下的模型:

var ex_table= sequelize.define("ex_table", {
concept1: Sequelize.STRING(5),
concept2: Sequelize.STRING(80),
concept3: Sequelize.STRING,
weight: Sequelize.INTEGER,
value: Sequelize.DECIMAL(20,2)
}, {
tableName: "samble_table"});

从表中检索单行是可行的,我这样做:

ex_table
.find({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})})
.complete(function (err, result) {
if (!!err) {
console.log("An error occurred while creating the table:", err);
} else {
console.log(result.values);
}
})

这给了我一行数据,这符合预期。但是,当我尝试像这样检索多行时:

ex_table
.findAll({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})})
.complete(function (err, result) {
if (!!err) {
console.log("An error occurred while creating the table:", err);
} else {
console.log(result.values);
}
})

在这里我感到不明白,有什么想法如何获取多行吗?

最佳答案

我发现,至少有两种方法可以获取所有行:正如 @barry-johnson 所评论的,结果是一个数组,因此我们像其他数组一样遍历该数组:

ex_table
.findAll({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})})
.complete(function (err, result) {
if (!!err) {
console.log("An error occurred while creating the table:", err);
} else {
for (var i=0; i<result.length; i++) {
console.log(result[i].values);
}
}
})

或者在 Sequelize 文档的模型部分有关于原始查询的有趣的一行:

Sometimes you might be expecting a massive dataset that you just want to display, without manipulation. For each row you select, Sequelize creates a DAO, with functions for update, delete, get associations etc. If you have thousands of rows, this might take some time. If you only need the raw data and don't want to update anything, you can do like this to get the raw data.

因为我想查看原始数据,这就是我们所需要的,所以我们可以获得像这样的多行:

ex_table
.findAll({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})}, {raw:true})
.complete(function (err, result) {
if (!!err) {
console.log("An error occurred while creating the table:", err);
} else {
console.log(result);
}
})

希望这对其他人也有帮助。

关于node.js - 使用 Sequelizejs 检索多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23473707/

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