1

Today I open my tiny app I'm using to learn Node.js with, together with Express and Sequelize.JS, but I suddenly encounter a problem, every time I want to log some object or array of objects or something like that (my app is tiny and also its objects) it takes really long and when the log comes it is monstrously long (of course that is syncronous so app freezes). It's like:

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'));   
}

Any clue why this is suddenly happening, even trying to log a single product, could it be related to the relationships of the models? i cannot think of anything.

This is the last piece of log, i dare to say it's millions of lines:

            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],
3
  • Is it better when you write console.log(products.toJSON()); ? Commented Dec 16, 2018 at 3:41
  • that is not the problem, the logs are too long, that's not normal, and the time it takes...., i'm on an i7 mac Commented Dec 16, 2018 at 3:58
  • By logs are too long, do you mean it is printing things that are not in products? Commented Dec 16, 2018 at 4:06

2 Answers 2

2

Your problem is each object or array of objects retrieved from database via Sequelize (ORM with Node.JS), are instances which sequelize managing, when you want to console log to view data, you should try toJSON() which Sequelize support. The document here http://docs.sequelizejs.com/class/lib/model.js~Model.html#instance-method-toJSON, that is instance method to JSON

Your code should be:

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

I've also test and it's working with map and toJSON(). Can you share more things about your code?

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Can you share what req.user store, is that a Sequelize instance?
@MarcosDiPaolo, problem because the products is array of instances, so we should use map each instance to log. I've edited my solution. You can try that.
i cannot, i cannot log anything, also not the req.user, i have to wait a long time and then this millions of lines get logged at once. If i log console.log(products.length) then it works and gives me the right length, but the product itself is impossible. My console crashes.
Can you share what is logging on your console?
@MarcosDiPaolo try JSON.stringify(products)
|
1

So here you are getting instances of sequelize that is why it is printing lots of other information. Check Sequelize Instances for more info. To get the value of the instance try {plain: true}. So your above code will become

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')))
}

Check about value of instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.