在 Sequelize 中得到很多结果
Get many results in Sequelize
如何在数组中获取多个Sequelize结果?示例:我需要在控制台中获取 table test
和 return 中的所有值字段 name
。我写:
test.findAll().them(function(result) {
result.forEach(function(item) {
console.log(item.name);
});
});
如何在没有 forEach()
的情况下获取数组中的所有值字段 name
?
(抱歉英语不好)
test.findAll({attributes: ['name']}).them(function(result) {
console.log(result);
});
您可以使用 map
将名称提取到数组中。
test.findAll().then(function(result) {
var names = result.map(function(item) {
return item.name;
});
console.log(names);
});
如果您担心数据库会返回您不关心的其他字段,您可以对 findAll
使用 attributes
选项,如 所述:
test.findAll( {attributes: ['name']} ).then(function(result) {
var names = result.map(function(item) {
return item.name;
});
console.log(names);
});
如何在数组中获取多个Sequelize结果?示例:我需要在控制台中获取 table test
和 return 中的所有值字段 name
。我写:
test.findAll().them(function(result) {
result.forEach(function(item) {
console.log(item.name);
});
});
如何在没有 forEach()
的情况下获取数组中的所有值字段 name
?
(抱歉英语不好)
test.findAll({attributes: ['name']}).them(function(result) {
console.log(result);
});
您可以使用 map
将名称提取到数组中。
test.findAll().then(function(result) {
var names = result.map(function(item) {
return item.name;
});
console.log(names);
});
如果您担心数据库会返回您不关心的其他字段,您可以对 findAll
使用 attributes
选项,如
test.findAll( {attributes: ['name']} ).then(function(result) {
var names = result.map(function(item) {
return item.name;
});
console.log(names);
});