Javascript 来自 Sequelize 的 findAll() 没有得到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36214221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:47:22  来源:igfitidea点击:

findAll() from Sequelize doesn't get

javascriptmysqlsequelize.js

提问by German Gonzalez

I'm using Sequelize with MySQL.

我在 MySQL 中使用 Sequelize。

When I run this code:

当我运行此代码时:

usuarioService.getAll = function () {
    Usuario.findAll().then(function (users) {
        //return users;
        console.dir(users);
    });
}

Instead of get the user, I get:

我没有得到用户,而是得到:

http://i.stack.imgur.com/uLhmN.png

http://i.stack.imgur.com/uLhmN.png

Help me, please! I'm going crazy!

请帮帮我!我要疯了!

Thanks

谢谢

回答by hargasinski

Sequelize is returning an array of instanceobjects in users. An instanceobject has a number of convenience methods attached to it that allow you to act on it.

Sequelize 正在返回instance用户中的对象数组。一个instance对象附有许多方便的方法,允许您对其进行操作。

If you want to get just the data with your fields as keys, use get({plain: true}). For example, for the first object in the array users[0].get({plain: true}). If you want to keep using the instances, you can just use get with the name of your field. For example, users[0].get('nombre').

如果您只想获取以字段为键的数据,请使用get({plain: true}). 例如,对于数组中的第一个对象users[0].get({plain: true})。如果您想继续使用这些实例,您可以使用带有您的字段名称的 get。例如,users[0].get('nombre')

You should be also able to access the properties directly on the object, even if they're not being logged, such as users[0].nombre.

您还应该能够直接访问对象上的属性,即使它们没有被记录,例如users[0].nombre.

Edit

编辑

This is not related to the original question, but your comment on another answer. Make sure you are doing things asynchronously. The code should be:

这与原始问题无关,而是您对另一个答案的评论。确保您正在异步执行操作。代码应该是:

usuarioService.getAll = function (cb) {
    Usuario.findAll().then(function (users) {
        return cb(null, users);
    }).catch(function(err) {
        return cb(err);
    });
}

Then when calling this method you would do something like:

然后在调用此方法时,您将执行以下操作:

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)
    });
});

or

或者

Since Sequelize uses promises, doing this using promises would be the best way.

由于 Sequelize 使用 Promise,因此使用 Promise 将是最好的方法。

usuarioService.getAll = function () {
    return Usuario.findAll({ raw: true });
}

Then when calling this method you would do something like:

然后在调用此方法时,您将执行以下操作:

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
    });
});

回答by LT-

You are returning a user.

您正在返回用户。

The first bit you see is the SQL query that Sequelize is executing for you.

您首先看到的是 Sequelize 为您执行的 SQL 查询。

The bit that says

说的那一点

dataValues: 
   { usuario_id: 1,
    ... 
   }

is your user. findAll()should give you an array with all of your users.

是你的用户。findAll()应该给你一个包含所有用户的数组。

If you just want the dataValues returned you can just pass in raw: true.

如果您只想返回 dataValues,则只需传入raw: true.

usuarioService.getAll = function () {
    Usuario.findAll({ raw: true }).then(function (users) {
        //return users;
        console.dir(users);
    });
}