Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if hasmany relation result is null it gives error if it has too hasmany relationship with count function #11239

Open
sanjeev-nyxa opened this issue Jul 28, 2019 · 4 comments
Labels
status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug

Comments

@sanjeev-nyxa
Copy link

What are you doing?

I have post model which hasmany reply model then reply hasmany replylike model.

return req.db.postv1.findOne({
    attributes: [
      'id',
      'message',
      'title',
      'description',
      'question',
      'duration',
      'expiration',
      
    ],
    include: [{
      model: req.db.user,
      as: 'user'
    }, {
      model: req.db.reply,
      as: 'reply',
      limit: 5,
      order: [['createdAt', 'DESC']],
      include: [{
        model: req.db.user,
        attributes: ['id', 'firstName', 'lastName', 'email', 'socialImage', 'profilePicture', 'institutionName']
      }, {
        model: req.db.like,
        as: 'replyLike',
        attributes: [
          [sequelize.fn('COUNT', sequelize.col('replyLike.id')), 'replyCount']
        ]
      }]
    },  {
      model: req.db.attachment,
      attributes: ['id', 'cloudinaryPublicId']
    }, {
      model: req.db.pollOption,
      attributes: []
    }],
    where: {
      id: {
        [req.Op.eq]: postId
      },
      communityId: {
        [req.Op.eq]: communityId
      },
      courseId: {
        [req.Op.eq]: courseId
      }
    },
    group: ['id'],
    subQuery: false,
  })
  .then((post) => {
    req.$scope.post = post;
    next();
    return post;
  })
  .catch(error => {
    req.log.error({
      error: error
    }, 'handlers.post get-post-v1 [postv1.findOne] Error');

    return res.status(lib.httpCodes.SERVER_ERROR)
    .send(new lib.rpc.InternalError(error));
  });

To Reproduce
if I comment this line [sequelize.fn('COUNT', sequelize.col('replyLike.id')), 'replyCount'] it start working because this post has no reply yet so reply object is null and it gives "Cannot read property 'push' of undefined"

What do you expect to happen?

It should return reply object as [] empty array as it return when I comment count line!

What is actually happening?

It give me error because post has no reply so reply object is null and replyLike object is trying to push into reply object!

Output, either JSON or SQL
{
"status": "ERROR",
"statusCode": 100,
"statusMessage": "Cannot read property 'push' of undefined",
"httpCode": 500
}

@papb
Copy link
Member

papb commented Jul 28, 2019

You have posted a code snippet that shows the problem but is not entirely self-contained (i.e. I can't just copy-paste it and run it). Can you (or someone else) please provide a SSCCE (also known as MCVE/reprex)? This will help tackling the issue faster.

@papb papb added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug labels Jul 28, 2019
@sanjeev-nyxa
Copy link
Author

You have posted a code snippet that shows the problem but is not entirely self-contained (i.e. I can't just copy-paste it and run it). Can you (or someone else) please provide a SSCCE (also known as MCVE/reprex)? This will help tackling the issue faster.

It is quite impossible for me to give you any live demo to access or compile anything at the moment . Yes but I can elaborate more

I have tables in mysql
Posts
Reply
Likes

Post has replationship Has many to Reply
Reply has relationship Has many to likes

so in Posts model I have

this.hasMany(models.like, {
      as: 'like'
    });

and in Reply model I have

 this.hasMany(models.like, {
      as: 'replyLike'
    });

so it works fine if I have a at least one reply for a post if post has no reply then it start giving me error "Cannot read property 'push' of undefined"

so it looks like hasMany for like does not work on reply if reply has null or empty values..

I want to tell you I am using count in Like attributes other wise it works fine if I fetch all records from like table instead of total count

@papb
Copy link
Member

papb commented Jul 29, 2019

Thanks for the extra information!

I will keep the issue labeled as 'awaiting response' though, because we really need a SSCCE/MCVE/reprex to be able to work on it properly. When you have the time (or someone else), please update the issue and ping me. Thanks!

@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 8, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug
Projects
None yet
Development

No branches or pull requests

3 participants