0

When I run any find function with sequelize, I expect to get an object of the data I am trying to get, instead, I get a bunch of extra data back.This is what I get when I print the result of what sequelize gave me

This is the code i run:

getAllHubsByUser: function(userId, callback){
        db.User.findByPk(userId)
            .then(function(user) {
                user.getSubscriptions()
                    .then(function(hubs){
                        dataValuesHubs = []
                        for (hub in hubs) {
                            dataValuesMembers.push(hub.dataValues)
                        }
                        callback(dataValuesHubs, null)
                    })
                    .catch(function(error){
                        callback(null, error)
                    })
            })
            .catch(function(error) {
                callback(null, error)
            })
    },

I have a many to many relation between a table called Hubs and Users, many users can be subscribed to many hubs. The relations looks like this:

User.belongsToMany(Hub, {as: "Subscriptions", through: 'hubSubscriptions', foreignKey: 'userId'})
Hub.belongsToMany(User, {as: "Subscribers", through: 'hubSubscriptions', foreignKey: 'hubId'})

Since sequelize don't return a object, i get an error when u run user.getSubscriptions(): "Unhandled rejection TypeError: Cannot read property 'findAll' of undefined"

How can i make it so sequelize sends back a object?

eek
  • 57
  • 2
  • 7

2 Answers2

0

You just got a sequelize model object instance with its own additional props and methods. To get plain object do this:

const plainUser = user.get({ plain: true })
console.log(plainUser)
Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • I updated the question after i got you answere. What you suggested works correctly but it's not really my problem – eek Mar 18 '20 at 19:16
  • This way db.User.findByPk(userId, { include: [{ model: db.Hub, as: 'Subscriptions' }]) you'll get user with all hubs. Then just call const plainUser = user.get({ plain: true }) and you'll get all hubs in user.Subsriptions – Anatoly Mar 18 '20 at 20:53
  • Thanks, but when i subscribe to a hub as a user i create a new subscription very simular to what i do above. How do i create data new many to many (a new subscription) since i most likely will get the same error, i basically run hub.addSubscriber() the same way i run user.getSubscriptions() – eek Mar 18 '20 at 21:03
  • to call hub.addSubsriber() you should get hub with including db.User as Subscribers – Anatoly Mar 18 '20 at 21:09
0

As @Anatoly stated, you are getting Sequelize.Model object with all of it's props and methods initiated within it. To get the plain javascript object, you could use following implenentation

getUserByEmail: function(email, callback) {
        db.User.findOne({
            where: {email: email},
            raw: true // Get the raw object.
        })
            .then(function(user){
                console.log(user)
                if (user) {
                    callback(user.dataValues, null)
                } else {
                    callback(null, null)
                }
            })
            .catch(function(error){
                console.log(error)
                callback(null, error)
            })
}

If you want all query should return plain object by default, then check this answer.

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • I updated my question after i got @Anatoly question. Both your and his solution works but it's not really the problem i have, i hope my question is easier to understand now – eek Mar 18 '20 at 19:18