简体   繁体   English

JavaScript承诺不返回对象

[英]JavaScript Promises Not Returning Object

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. 我在使用Sequelize来运行查询时遇到一个问题,在该语句中,第一个然后是语句返回对象,然后在另一个表上运行查询。 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. 运行该查询后,我还有一条额外的then语句,该语句应传递返回的对象以能够在我的视图中访问。 Unfortunately, the second query does not return an object as the console log returns undefined for the second log (Check console.log(member) ). 不幸的是,第二个查询没有返回对象,因为控制台日志为第二个日志返回undefined (请检查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);
        })
    });

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. 您正在返回organization ,从你的那句话,所以未来then获取它的价值。 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);
        })
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM