问题
In sequelize if you do a
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));
or
res.json(dbTodo);
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).
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?
I've searched through the sequelize docs and haven't found a good explanation.
回答1:
There are two ways this behavior could manifest.
The missing properties are non-enumerable. Properties defined by Object.defineProperty set to
enumerable: false(or simply don't supply a value for theenumerablesetting) are non-enumerable properties, and are ignored byJSON.stringify. You can check a property'senumerablesetting in the output of Object.getOwnPropertyDescriptor(obj, propName).The object has (or has in its prototype chain) a toJSON method:
If an object being stringified has a property named
toJSONwhose value is a function, then thetoJSON()method customizes JSON stringification behavior: instead of the object being serialized, the value returned by thetoJSON()method when called will be serialized.If the object has or inherits a
toJSONmethod, then the return value of that method will be the target for stringification. You can check if one exists on some objectobjsimply by reading the value ofobj.toJSON.
来源:https://stackoverflow.com/questions/41747844/how-does-the-sequelize-pojo-json-stringify-magic-work