question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unhandled rejection Error: User (friends) is not associated to SNS!

See original GitHub issue

I’m trying to design data model for represent network for user and user’s friends

basically, there are two model. User for store user’s own information. and SNS for store user’s relationship.

here is my

var User = sequelize.define(
    'User', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        name: DataTypes.STRING,
        email: DataTypes.STRING(256),
        pwd: DataTypes.STRING(2048),
        avatar: DataTypes.STRING(2048),
        property: DataTypes.JSON,
        preference: DataTypes.JSON
    })

var SNS = sequelize.define("SNS", {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    status: DataTypes.INTEGER
})

here is the code i added for association models.User.belongsToMany(models.User, { as: ‘friends’, foreignKey: “userId”, through: models.SNS });

            models.User.belongsToMany(models.User, {
                as: 'networks',
                foreignKey: "friendId",
                through: models.SNS
            });

when i try to cal findAll() with “include” , i got a error. (refer to code below)

SNS.findAll({
        include: [{
            model: models.User,
            as: 'friends'
        }]
    })
    .then(function(result) {
        res.send(result);
    });

here is the error Unhandled rejection Error: User (friends) is not associated to SNS!

what’s wrong with my model definition. I could a execute a query with include.

The findAll work if i remove the include part. but in that case i will get only userID in SNS model. however i want a user object as well. i don’t want to do a query again.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
seraphjiangcommented, Oct 22, 2015

thanks Jameier, it works, thanks for help.

here is association models.SNS.belongsTo(models.User, { foreignKey: “userId” });

            models.SNS.belongsTo(models.User, {
                foreignKey: "friendId", as: "Friend"
            });

here is the query

SNS.findAll({
        include: [{
            model: models.User,
            as: 'User'
        }, {
            model: models.User,
            as: "Friend"
        }]
    })
    .then(function(result) {
        res.send(result);
    });
2reactions
janmeiercommented, Oct 22, 2015

You need to add { as: friends} so that you have to same as (or none) on both the model and the association

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve authorization errors when adding subscribers to an ...
To resolve the error, grant the IAM entity permission to run the Subscribe API action on the Amazon SNS topic. If you receive...
Read more >
Resolve error: Unhandled Rejection (Error): Network Error
Try to run the App using Chrome in Incognito mode. This way it will prompt for user login where you can provide the...
Read more >
Configuring region in Node.js AWS SDK - Stack Overflow
I've followed all the examples from the aws doc page but I still get this error no matter what. { [ConfigError: Missing region...
Read more >
AWS-Amplify/Lobby - Gitter
Getting the following error access files using Storage. Logged in using Auth.signin. Not sure why I am still getting this error. Is this...
Read more >
Node.js Agent version 3.x - Elastic
on transactions (and captured errors) for Lambda functions triggered by API ... If you use that package, you should not use v3.28.0 of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found