如何使这个简单的For循环工作?

时间:2019-06-10 03:02:58

标签: nestjs typeorm

基本上,我正在尝试在“需求”和“ Pgroup”之间建立多对一关系。但是,就我的使用而言,我将获得创建请求,其中包含创建具有所有需求的一个Pg所需的所有数据。一枪像这样...

这是我一直在努力的代码
(一点背景)

    async new(data: PgroupEntity) {
    // const pgp = await this.pgrouprepository.create(    
    // await this.pgrouprepository.save(pgp);
        const pp = await this.pgrouprepository.findOne({ where: { id: 'c682620d-9717-4d3c-bef9-20a31d743a99' } });

这是代码的起始位置


        for (let item in data.needs ) {
            const need = await this.needrepository.create({...data.needs[item], pgroup: pp});
            await this.needrepository.save(need);
            return need;
        }
    }

由于某种原因,此for循环不起作用。它只迭代一次。下面的代码有效

const need = await this.needrepository.create({...data.needs[2], pgroup: pp});
        await this.needrepository.save(need);

但是我无法一次节省多个需求。

1 个答案:

答案 0 :(得分:-1)

尝试一下

async new(data: PgroupEntity) {
        // const pgp = await this.pgrouprepository.create(data);
        // await this.pgrouprepository.save(pgp);
        const pp = await this.pgrouprepository.findOne({ where: { id: 'bad6eb03-655b-4e29-8d70-7fd63a7fe7d7' } });
        data.needs.forEach(item => {
            const need = this.needrepository.create({...item, pgroup: pp});
            this.needrepository.save(need);
            return need;
        });
        return data;
    }
相关问题