来自Node.js的Sequelize中的问题console.log()对象

时间:2018-12-16 03:23:54

标签: node.js sequelize.js console.log

今天,我打开了用来学习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],

2 个答案:

答案 0 :(得分:1)

您的问题是通过Sequelize(带有Node.JS的ORM)从数据库中检索到的每个对象或对象数组都是序列化的实例,当您要控制台日志以查看数据时,应尝试{{ 1}}的支持。 http://docs.sequelizejs.com/class/lib/model.js~Model.html#instance-method-toJSON这里的文档,即toJSON()

您的代码应为:

instance method to JSON
  

公共toJSON():对象

     

将实例转换为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'))) } map一起使用。您可以分享更多有关代码的信息吗?

enter image description here

答案 1 :(得分:0)

所以在这里您得到的是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的信息。