使用 Node.js 的 Sequelize 中的问题 console.log() 对象

Problem console.log() object from Sequelize with Node.js

今天我打开我用来学习 Node.js 的小应用程序,连同 Express 和 Sequelize.JS,但我突然遇到一个问题,每次我想记录一些对象或对象数组或类似的东西(我的应用程序很小,它的对象也是)它需要很长时间,当日志出现时它非常长(当然这是同步的所以应用程序冻结)。就像:

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(ORM with Node.JS)从数据库中检索到的每个对象或对象数组,都是后续管理的实例,当你想控制台日志查看数据时,你应该尝试 toJSON() 支持 Sequelize。这里的文件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

我也进行了测试,它可以与 maptoJSON() 一起使用。你能分享更多关于你的代码的东西吗?

所以在这里你得到了 sequelize 的实例,这就是它打印大量其他信息的原因。检查 Sequelize Instances 了解更多信息。 要获取实例的值,请尝试 {plain: true}。所以你上面的代码会变成

exports.getCartPage = (req, res, next) => {
  req.user.getCart()
 .then(cart => cart.getProducts())
 .then(products => {
    products.map(p => console.log(p.get({ plain: true })));
    res.render('shop/cart', {
        pageTitle: 'Cart',
        page: 'cart',
        products
    });
 })
 .catch(err => console.log('err')))
}

查看value of instance