循环内创建查询

时间:2019-05-15 20:21:09

标签: javascript node.js express promise sequelize.js

在查询数据库,遍历结果并在此之后再遍历2次之前,我在数据库中创建新行时遇到问题。

function create() {
  League.findAll()
    .then(leagues => {
        return Promise.all(leagues).then(leagues => {
            for (const league of leagues) {
                Club.findAll({
                    where: {
                        leagueId: league.id
                    }
                }).then(clubs => {
                    return Promise.all(clubs).then(clubs => {
                        for (const club of clubs) {
                            for (const awayClub of clubs) {
                                if (club !== awayClub) {
                                    Game.create({
                                        date: new Date(),
                                        isLeagueGame: true,
                                        isCupGame: false,
                                        homeClub: club.id,
                                        awayClub: awayClub.id
                                    })
                                }
                            }
                        }
                    })
                })
                .catch(error => { return false; })
            }
        })
        .then(() => {
            return true;
        })

    }).catch(error => { return false; })
}

我确实这样调用此函数:

router.post('/create', async (req, res, next) => {
    const success = await myClass.create();
    if (success) {
        return res.status(201).json({ message: "League Games created!" });
    }

    return res.status(500).json({ message: "An error occured!" });
});

因此,当我在success语句之后登录undefined时,它总是await。我真的不知道我的错误在哪里,我对Promises和Sequelize还是陌生的。可能还需要在这里进行交易...

编辑:

  function create() {
    League.findAll()
        .then(leagues => {
            return Promise.all(leagues.map(async (league) => {
                let clubs = await Club.findAll({ where: { leagueId: league.id } });
                return Promise.all(clubs.map((club) => {
                    return Promise.all(clubs.map((awayClub) => {
                        if (club !== awayClub) {
                            return Game.create({
                                date: new Date(),
                                isLeagueGame: true,
                                isCupGame: false,
                                homeClub: club.id,
                                awayClub: awayClub.id
                            })
                        }
                    }))
                }))
            }))
        })
        .then(result => { 
            console.log("All Games created!");
            return true;
        })
        .catch(error => { 
            console.log(error)
            return false;
        });
    }

现在对函数进行了平铺处理,但是当我调用它时,在我真正按下最后一个then块之前,似乎仍然返回了一些内容。另外,最后一个then块被调用,create被应创建的每个游戏调用,但是它们似乎没有被序列化到数据库中,至少在以前它能工作。

EDIT2:

async function create() {
    const leagues = await League.findAll();
    leagues.forEach(async (league) => {
        const clubs = await Club.findAll({ where: { leagueId: league.id }});
        clubs.forEach(async (club) => {
            clubs.forEach(async (awayClub) => {
                if (club !== awayClub) {
                    const result = await Game.create({
                        date: new Date(),
                        isLeagueGame: true,
                        isCupGame: false,
                        homeClub: club.id,
                        awayClub: awayClub.id
                    });
                }
            });
        });
    });
    return true;
}

现在让它像这样工作!

0 个答案:

没有答案