今天我打开我用来学习 Node.js 的小应用程序,连同 Express 和 Sequelize.JS,但我突然遇到一个问题,每次我想记录一些对象或对象数组或类似的东西(我的app 很小,还有它的对象)它需要很长时间,当日志出现时它非常长(当然这是同步的,所以应用程序卡住)。就好像是:
exports.getCartPage = (req, res, next) => {
req.user.getCart()
.then(cart => cart.getProducts()
.then(products => {
console.log(products); //!!!!!!!
res.render('shop/cart', {
pageTitle: 'Cart',
page: 'cart',
products: products
});
})
.catch(err => console.log('err')))
.catch(err => console.log('err'));
}
任何线索为什么会突然发生,甚至尝试记录单个产品,是否与模型的关系有关?我想不出什么。
这是最后一条日志,我敢说是几百万行:
attributes:
{ id:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
autoIncrement: true,
allowNull: false,
primaryKey: true,
Model: [Circular],
fieldName: 'id',
_modelAttribute: true,
field: 'id' },
createdAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'createdAt',
_modelAttribute: true,
field: 'createdAt' },
updatedAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'updatedAt',
_modelAttribute: true,
field: 'updatedAt' },
userId:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
allowNull: true,
references: { model: 'users', key: 'id' },
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'userId',
_modelAttribute: true,
field: 'userId' } },
tableAttributes:
{ id:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
autoIncrement: true,
allowNull: false,
primaryKey: true,
Model: [Circular],
fieldName: 'id',
_modelAttribute: true,
field: 'id' },
createdAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'createdAt',
_modelAttribute: true,
field: 'createdAt' },
updatedAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'updatedAt',
_modelAttribute: true,
field: 'updatedAt' },
userId:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
allowNull: true,
references: { model: 'users', key: 'id' },
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'userId',
_modelAttribute: true,
field: 'userId' } },
primaryKeyAttributes: [ 'id' ],
primaryKeyAttribute: 'id',
primaryKeyField: 'id',
_hasPrimaryKeys: true,
_isPrimaryKey:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
autoIncrementAttribute: 'id',
_scope: {},
_scopeNames: [ 'defaultScope' ] },
{ [Function: orderProduct]
sequelize: [Circular],
options:
{ timestamps: true,
validate: {},
freezeTableName: false,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
indexes: [],
name: { plural: 'orderProducts', singular: 'orderProduct' },
omitNull: false,
sequelize: [Circular],
hooks: {},
uniqueKeys:
{ orderProducts_productId_orderId_unique:
{ fields: [ 'orderId', 'productId' ],
msg: null,
name: 'orderProducts_productId_orderId_unique',
column: 'productId',
customIndex: true } } },
associations: {},
underscored: undefined,
tableName: 'orderProducts',
_schema: null,
_schemaDelimiter: '',
rawAttributes:
{ id:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
autoIncrement: true,
allowNull: false,
primaryKey: true,
Model: [Circular],
fieldName: 'id',
_modelAttribute: true,
field: 'id' },
qty:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
Model: [Circular],
fieldName: 'qty',
_modelAttribute: true,
field: 'qty' },
createdAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'createdAt',
_modelAttribute: true,
field: 'createdAt' },
updatedAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'updatedAt',
_modelAttribute: true,
field: 'updatedAt' },
orderId:
{ _autoGenerated: true,
type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
unique: 'orderProducts_productId_orderId_unique',
references: { model: 'orders', key: 'id' },
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'orderId',
_modelAttribute: true,
field: 'orderId' },
productId:
{ _autoGenerated: true,
type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
unique: 'orderProducts_productId_orderId_unique',
references: { model: 'products', key: 'id' },
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'productId',
_modelAttribute: true,
field: 'productId' } },
primaryKeys:
{ id:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
autoIncrement: true,
allowNull: false,
primaryKey: true,
Model: [Circular],
fieldName: 'id',
_modelAttribute: true,
field: 'id' } },
_timestampAttributes: { createdAt: 'createdAt', updatedAt: 'updatedAt' },
_readOnlyAttributes: [ 'createdAt', 'updatedAt' ],
_hasReadOnlyAttributes: 2,
_isReadOnlyAttribute:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
_dataTypeChanges:
{ createdAt: [Function: _isChanged],
updatedAt: [Function: _isChanged] },
_dataTypeSanitizers:
{ createdAt: [Function: _sanitize],
updatedAt: [Function: _sanitize] },
_booleanAttributes: [],
_dateAttributes: [ 'createdAt', 'updatedAt' ],
_hstoreAttributes: [],
_rangeAttributes: [],
_jsonAttributes: [],
_geometryAttributes: [],
_virtualAttributes: [],
_defaultValues: {},
fieldRawAttributesMap:
{ id:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
autoIncrement: true,
allowNull: false,
primaryKey: true,
Model: [Circular],
fieldName: 'id',
_modelAttribute: true,
field: 'id' },
qty:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
Model: [Circular],
fieldName: 'qty',
_modelAttribute: true,
field: 'qty' },
createdAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'createdAt',
_modelAttribute: true,
field: 'createdAt' },
updatedAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'updatedAt',
_modelAttribute: true,
field: 'updatedAt' },
orderId:
{ _autoGenerated: true,
type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
unique: 'orderProducts_productId_orderId_unique',
references: { model: 'orders', key: 'id' },
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'orderId',
_modelAttribute: true,
field: 'orderId' },
productId:
{ _autoGenerated: true,
type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
unique: 'orderProducts_productId_orderId_unique',
references: { model: 'products', key: 'id' },
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'productId',
_modelAttribute: true,
field: 'productId' } },
fieldAttributeMap: {},
uniqueKeys:
{ orderProducts_productId_orderId_unique:
{ fields: [ 'orderId', 'productId' ],
msg: null,
name: 'orderProducts_productId_orderId_unique',
column: 'productId',
customIndex: true } },
_hasBooleanAttributes: false,
_isBooleanAttribute:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
_hasDateAttributes: true,
_isDateAttribute:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
_hasHstoreAttributes: false,
_isHstoreAttribute:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
_hasRangeAttributes: false,
_isRangeAttribute:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
_hasJsonAttributes: false,
_isJsonAttribute:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
_hasVirtualAttributes: false,
_isVirtualAttribute:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
_hasGeometryAttributes: false,
_isGeometryAttribute:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
_hasDefaultValues: false,
attributes:
{ id:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
autoIncrement: true,
allowNull: false,
primaryKey: true,
Model: [Circular],
fieldName: 'id',
_modelAttribute: true,
field: 'id' },
qty:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
Model: [Circular],
fieldName: 'qty',
_modelAttribute: true,
field: 'qty' },
createdAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'createdAt',
_modelAttribute: true,
field: 'createdAt' },
updatedAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'updatedAt',
_modelAttribute: true,
field: 'updatedAt' },
orderId:
{ _autoGenerated: true,
type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
unique: 'orderProducts_productId_orderId_unique',
references: { model: 'orders', key: 'id' },
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'orderId',
_modelAttribute: true,
field: 'orderId' },
productId:
{ _autoGenerated: true,
type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
unique: 'orderProducts_productId_orderId_unique',
references: { model: 'products', key: 'id' },
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'productId',
_modelAttribute: true,
field: 'productId' } },
tableAttributes:
{ id:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
autoIncrement: true,
allowNull: false,
primaryKey: true,
Model: [Circular],
fieldName: 'id',
_modelAttribute: true,
field: 'id' },
qty:
{ type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
Model: [Circular],
fieldName: 'qty',
_modelAttribute: true,
field: 'qty' },
createdAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'createdAt',
_modelAttribute: true,
field: 'createdAt' },
updatedAt:
{ type: DATE { options: { length: undefined }, _length: '' },
allowNull: false,
_autoGenerated: true,
Model: [Circular],
fieldName: 'updatedAt',
_modelAttribute: true,
field: 'updatedAt' },
orderId:
{ _autoGenerated: true,
type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
unique: 'orderProducts_productId_orderId_unique',
references: { model: 'orders', key: 'id' },
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'orderId',
_modelAttribute: true,
field: 'orderId' },
productId:
{ _autoGenerated: true,
type:
INTEGER {
options: { length: undefined },
_length: undefined,
_zerofill: undefined,
_decimals: undefined,
_precision: undefined,
_scale: undefined,
_unsigned: undefined },
unique: 'orderProducts_productId_orderId_unique',
references: { model: 'products', key: 'id' },
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
Model: [Circular],
fieldName: 'productId',
_modelAttribute: true,
field: 'productId' } },
primaryKeyAttributes: [ 'id' ],
primaryKeyAttribute: 'id',
primaryKeyField: 'id',
_hasPrimaryKeys: true,
_isPrimaryKey:
{ [Function: memoized]
cache:
MapCache {
size: 0,
__data__:
{ hash: Hash { __data__: [Object: null prototype] {}, size: 0 },
map: Map {},
string: Hash { __data__: [Object: null prototype] {}, size: 0 } } } },
autoIncrementAttribute: 'id',
_scope: {},
_scopeNames: [ 'defaultScope' ] } ],
sequelize: [Sequelize] },
connectionManager: [ConnectionManager],
importCache: [Object],
test: [Object] },
hooks: [Object],
最佳答案
您的问题是通过 Sequelize
(带有 Node.JS 的 ORM)从数据库中检索的每个对象或对象数组,都是后续管理的实例,当您想控制台日志查看数据时,您应该尝试 Sequelize 支持的 toJSON()
。这里的文档是 http://docs.sequelizejs.com/class/lib/model.js~Model.html#instance-method-toJSON ,也就是 instance method to JSON
你的代码应该是:
exports.getCartPage = (req, res, next) => {
req.user.getCart()
.then(cart => cart.getProducts())
.then(products => {
products.map(p => console.log(p.toJSON()));
res.render('shop/cart', {
pageTitle: 'Cart',
page: 'cart',
products
});
})
.catch(err => console.log('err')))
}
public toJSON(): object
Convert the instance to a JSON representation. Proxies to calling get with no keys. This means get all values gotten from the DB, and apply all custom getters.
Return: object
我也测试过,它与
map
和 toJSON()
一起工作。你能分享更多关于你的代码的东西吗?关于node.js - 来自 Sequelize 和 Node.js 的问题 console.log() 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53799177/