postgresql 如何在 Sequelize 中以 JSON 形式返回数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29869262/
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-10-21 01:53:14  来源:igfitidea点击:

How to return data as JSON in Sequelize

node.jspostgresqlsequelize.js

提问by Andy Ray

When I make a Sequelize query it returns to me an object (or array) which I'm guessing is a Sequelize model (or array of Models (a collection type??)) but it's not documented anywhere, so I'm just guessing. I would always like the results to be JSON. Is there anything I can pass in the query to force this? I would prefer not to hand massage every result I get back to be JSON if possible.

当我进行 Sequelize 查询时,它返回给我一个对象(或数组),我猜它是一个 Sequelize 模型(或模型数组(集合类型??)),但它没有记录在任何地方,所以我只是猜测. 我总是希望结果是 JSON。我可以在查询中传递什么来强制执行此操作吗?如果可能的话,我不想手动按摩我得到的每个结果都是 JSON。

The documentationshow this to return a string:

文档显示这是返回一个字符串:

console.log(JSON.stringify(users))

So there's some built in serialization. Right now I'm doing this, using the undocumented toJSON()method:

所以有一些内置的序列化。现在我正在这样做,使用未记录的toJSON()方法:

query().then(function(result) {
    if(result.length) {
        return result.toJSON();
    } else {
        return result.map(function(item) {
            return item.toJSON();
        });
    }
});

which is cumbersome.

这很麻烦。

回答by captainkirk

When you retrieve the model from the database, you can call the .get({ plain: true})on the result and it will handle the conversion for you. You can assign the value of the function call to a variable. For example

当您从数据库中检索模型时,您可以.get({ plain: true})对结果调用,它将为您处理转换。您可以将函数调用的值分配给变量。例如

..).then(function(user){ var _user = user.get({ plain: true}); console.log(_user); //Should be valid json object });

..).then(function(user){ var _user = user.get({ plain: true}); console.log(_user); //Should be valid json object });

Hope this helps.

希望这可以帮助。

回答by Hoverbear

You can use raw: truein the Query however this does not always behave as you might expect, especially with associations.

您可以raw: true在 Query 中使用,但是这并不总是如您所愿,尤其是关联。

query({
    // ...
    raw: true
}).then(function(result) {
    // Result is JSON!
});

However in the case where you're using associations you may get something like this:

但是,在您使用关联的情况下,您可能会得到如下信息:

{
    foo: true,
    "associated.bar": true
}

Instead of what you might expect:

而不是您可能期望的:

{
    foo: true,
    associated: {
        bar: true
    }
}

回答by Lucas

If you're doing a query with which has multiple results you should expect an array to be returned. You should find that each element in the result array is a JSON object.

如果您正在执行具有多个结果的查询,您应该期望返回一个数组。您应该会发现结果数组中的每个元素都是一个 JSON 对象。

You should be able to access a given field like this: result[0].myfieldname

您应该能够像这样访问给定的字段: result[0].myfieldname