Sequelize POJO JSON.stringify 魔法是如何工作的? [英] How does the Sequelize POJO JSON.stringify magic work?

查看:60
本文介绍了Sequelize POJO JSON.stringify 魔法是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你做了一个续集

db.MyTable.findAll({}).function(response){
    console.log(response);
}

您将看到如下所示的输出(当然取决于您的表格的外观):

You will see output that looks like this (depending on what your table looks like, of course):

[ { dataValues:
     { id: 1,
       text: 'sdf',
       complete: false,
       createdAt: Thu Jan 19 2017 11:55:38 GMT-0500 (Eastern Standard Time),
       updatedAt: Thu Jan 19 2017 11:55:38 GMT-0500 (Eastern Standard Time) },
    _previousDataValues:
     { id: 1,
       text: 'sdf',
       complete: false,
       createdAt: Thu Jan 19 2017 11:55:38 GMT-0500 (Eastern Standard Time),
       updatedAt: Thu Jan 19 2017 11:55:38 GMT-0500 (Eastern Standard Time) },
    _changed: {},
    '$modelOptions':
     { timestamps: true,
       instanceMethods: {},
       classMethods: {},
       validate: {},
       freezeTableName: false,
       underscored: false,
       underscoredAll: false,
       paranoid: false,
       rejectOnEmpty: false,
       whereCollection: null,
       schema: null,
       schemaDelimiter: '',
       defaultScope: {},
       scopes: [],
       hooks: {},
       indexes: [],
       name: [Object],
       omitNul: false,
       sequelize: [Object],
       uniqueKeys: {},
       hasPrimaryKeys: true },
    '$options':
     { isNewRecord: false,
       '$schema': null,
       '$schemaDelimiter': '',
       raw: true,
       attributes: [Object] },
    hasPrimaryKeys: true,
    __eagerlyLoadedAssociations: [],
    isNewRecord: false } ]

可以说,它是一个巨大的复杂对象,上面有一堆元数据.

Suffice to say, it is a big complex object with a bunch of metaData on it.

但是,如果您尝试使用以下任一方法将复杂的大对象转换为字符串:

However, if you try and turn that big complicated object into a string using either:

console.log(JSON.stringify(dbTodo));

res.json(dbTodo);

您将只返回实际实体的信息(大型复杂对象的 dataValues 属性中的项目).对于我这里的测试表,它看起来像这样:

you will get back just the information for the actual entity (the items in the dataValues property of the big complex object). For my test table here, it looks like this:

[{
    "id":1,
    "text":"sdf",
    "complete":false,
    "createdAt":"2017-01-19T16:55:38.000Z",
    "updatedAt":"2017-01-19T16:55:38.000Z"
 }]

这明显更简单(而且正是我想要的).

Which is significantly simpler (and just what I want).

这个魔法是怎么发生的?sequelize 是否创建了自己的 JSON.stringify() 版本和 express' res.json?

How does this magic happen? Is sequelize creating their own versions of JSON.stringify() and express' res.json?

什么时候发生,什么时候不发生?

What are the rules around when it happens and when it doesn't happen?

我搜索了 sequelize docs 并没有找到好的解释.

I've searched through the sequelize docs and haven't found a good explanation.

推荐答案

这种行为可以通过两种方式表现出来.

There are two ways this behavior could manifest.

  1. 缺失的属性是不可枚举的.由 Object.defineProperty 设置为 enumerable: false(或者只是不为 enumerable 设置提供值)是不可枚举的属性,并且被 <代码>JSON.stringify.您可以在 Object.getOwnPropertyDescriptor(obj, propName).

  1. The missing properties are non-enumerable. Properties defined by Object.defineProperty set to enumerable: false (or simply don't supply a value for the enumerable setting) are non-enumerable properties, and are ignored by JSON.stringify. You can check a property's enumerable setting in the output of Object.getOwnPropertyDescriptor(obj, propName).

该对象具有(或在其原型链中)一个 toJSON 方法:

The object has (or has in its prototype chain) a toJSON method:

如果一个被字符串化的对象有一个名为 toJSON 的属性,其值为一个函数,那么 toJSON() 方法会自定义 JSON 字符串化行为:而不是对象被序列化,toJSON() 方法在调用时返回的值将被序列化.

If an object being stringified has a property named toJSON whose value is a function, then the toJSON() method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON() method when called will be serialized.

如果对象有或继承了toJSON方法,则该方法的返回值将成为字符串化的目标.您可以通过读取 obj.toJSON 的值来检查某个对象是否存在 obj.

If the object has or inherits a toJSON method, then the return value of that method will be the target for stringification. You can check if one exists on some object obj simply by reading the value of obj.toJSON.

这篇关于Sequelize POJO JSON.stringify 魔法是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆