来自 Sequelize 的 findAll() 没有得到
findAll() from Sequelize doesn't get
我正在使用 MySQL 的 Sequelize。
当我运行这段代码时:
usuarioService.getAll = function () {
Usuario.findAll().then(function (users) {
//return users;
console.dir(users);
});
}
我得到的不是用户,而是:
http://i.stack.imgur.com/uLhmN.png
请帮帮我!我要疯了!
谢谢
您正在返回一个用户。
您首先看到的是 Sequelize 正在为您执行的 SQL 查询。
dataValues:
{ usuario_id: 1,
...
}
是您的用户。 findAll()
应该给你一个包含所有用户的数组。
如果您只想返回数据值,您可以直接传入 raw: true
。
usuarioService.getAll = function () {
Usuario.findAll({ raw: true }).then(function (users) {
//return users;
console.dir(users);
});
}
Sequelize 正在返回用户中 instance
个对象的数组。 instance
对象有许多附加的方便方法,允许您对其进行操作。
如果您只想获取以您的字段作为键的数据,请使用 get({plain: true})
。例如,对于数组中的第一个对象 users[0].get({plain: true})
。如果你想继续使用这些实例,你可以只使用 get 和你的字段名称。例如,users[0].get('nombre')
.
您还应该能够直接访问对象的属性,即使它们没有被记录,例如 users[0].nombre
。
编辑
这与原问题无关,而是您对另一个答案的评论。确保您正在异步执行操作。代码应该是:
usuarioService.getAll = function (cb) {
Usuario.findAll().then(function (users) {
return cb(null, users);
}).catch(function(err) {
return cb(err);
});
}
然后在调用这个方法时你会做这样的事情:
router.get('your_path', function(req, res, next) {
serv.getAll(function(err, users) {
if (err) {
// your err handling code
}
// users is now a valid js array
// could send it in res.json(users)
});
});
或
由于 Sequelize 使用 promises,因此使用 promises 将是最好的方法。
usuarioService.getAll = function () {
return Usuario.findAll({ raw: true });
}
然后在调用这个方法时你会做这样的事情:
router.get('your_path', function(req, res, next) {
serv.getAll().then(function(users) {
res.render('usuarios/index',{
users: users
})
}).catch(function(err) {
// your error handling code here
});
});
我正在使用 MySQL 的 Sequelize。
当我运行这段代码时:
usuarioService.getAll = function () {
Usuario.findAll().then(function (users) {
//return users;
console.dir(users);
});
}
我得到的不是用户,而是:
http://i.stack.imgur.com/uLhmN.png
请帮帮我!我要疯了!
谢谢
您正在返回一个用户。
您首先看到的是 Sequelize 正在为您执行的 SQL 查询。
dataValues:
{ usuario_id: 1,
...
}
是您的用户。 findAll()
应该给你一个包含所有用户的数组。
如果您只想返回数据值,您可以直接传入 raw: true
。
usuarioService.getAll = function () {
Usuario.findAll({ raw: true }).then(function (users) {
//return users;
console.dir(users);
});
}
Sequelize 正在返回用户中 instance
个对象的数组。 instance
对象有许多附加的方便方法,允许您对其进行操作。
如果您只想获取以您的字段作为键的数据,请使用 get({plain: true})
。例如,对于数组中的第一个对象 users[0].get({plain: true})
。如果你想继续使用这些实例,你可以只使用 get 和你的字段名称。例如,users[0].get('nombre')
.
您还应该能够直接访问对象的属性,即使它们没有被记录,例如 users[0].nombre
。
编辑
这与原问题无关,而是您对另一个答案的评论。确保您正在异步执行操作。代码应该是:
usuarioService.getAll = function (cb) {
Usuario.findAll().then(function (users) {
return cb(null, users);
}).catch(function(err) {
return cb(err);
});
}
然后在调用这个方法时你会做这样的事情:
router.get('your_path', function(req, res, next) {
serv.getAll(function(err, users) {
if (err) {
// your err handling code
}
// users is now a valid js array
// could send it in res.json(users)
});
});
或
由于 Sequelize 使用 promises,因此使用 promises 将是最好的方法。
usuarioService.getAll = function () {
return Usuario.findAll({ raw: true });
}
然后在调用这个方法时你会做这样的事情:
router.get('your_path', function(req, res, next) {
serv.getAll().then(function(users) {
res.render('usuarios/index',{
users: users
})
}).catch(function(err) {
// your error handling code here
});
});