0

I have an issue where I am using Sequelize to run a query and within the first then statment return the object and then run a query on a different table. After that query is run I have an additional then statement that should pass the returned objects to be able to access in my view. Unfortunately, the second query does not return an object as the console log returns undefined for the second log (Check console.log(member)). Any reason why this is happening or what I am doing wrong? The queries come back with the right statements.

appRoutes.route('/settings/add-users')

    .get(function(req, res){
        models.Organization.find({
            where: {
                organizationId: req.user.organizationId
            }, attributes: ['organizationId', 'organizationName','admin','members']
        }).then(function(organization, member){
            models.Member.findAll({
                where: {
                    organizationId: organization.organizationId
                }, attributes: ['memberEmail']
            });
            return organization;
        }).then(function(organization, member){

            console.log(organization);
            console.log(member);
            res.render('pages/app/add-users.hbs',{
                user: req.user,
                organization: organization,
                member: member
            });
        }).catch(function(error){
            res.send(error);
            console.log(error);
        })
    });
1
  • That's because the result of the resolved value of the second promise is the return value of the callback. In this case, it's organization. Perhaps you wanted to return models.member.findAll()? Commented Jan 19, 2016 at 2:32

1 Answer 1

1

A promise can only resolve to a single value. You are returning organization again from your then, so the next then gets that as its value. You can use an object to achieve the behaviour you want.

appRoutes.route('/settings/add-users')

    .get(function(req, res){
        var organization;

        models.Organization.find({
            where: {
                organizationId: req.user.organizationId
            }, attributes: ['organizationId', 'organizationName','admin','members']
        }).then(function(_organization){
            organization = _organization;
            return models.Member.findAll({
                where: {
                    organizationId: organization.organizationId
                }, attributes: ['memberEmail']
            });
        }).then(function(member){

            console.log(organization);
            console.log(member);
            res.render('pages/app/add-users.hbs',{
                user: req.user,
                organization: organization,
                member: member
            });
        }).catch(function(error){
            res.send(error);
            console.log(error);
        })
    });
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer! It appears that an error is thrown at the return object where there is a , after organization,. Unexpected token. Any thoughts on why this occurred?
@cphill Oh my bad, I assumed that you were doing this in a recent version of node.js. That is an ES6 only syntax ( it is shorthand for organization: organization ). I will edit the answer.
No worries! although I don't believe this solution worked because I receive [object Promise] as the logged object. I'm curious if the location of the log is the reason why that is happening. I notice that my models.Member.findAll query is being triggered after the console log of results.members Any idea why that might be?
@cphill You're right, I missed something let me start over.
@cphill My new answer should work for you, although I don't find the outer scoped organization variable particularly nice. That is how I typically get around this issue.
|

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.