dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

Async call inside for loop #692

Closed pito-svk closed 8 years ago

pito-svk commented 8 years ago

I have a problem with using node-orm in 'for loop':

for (let i = quantity - 1; i >= 0; i--) {
    FirstModel.create(firstModelData, (err, firstModel) => {
        if (err) {
            ...
        }

        secondModel.addFirstModel(firstModel, err) => {
            if (err) {
                ...
            }
        }
    });
}

I want to create arbitrary number of FirstModel items (works).

But, I want to assign each one to secondModel item. (doesn't work).

Problem is that for loop evaluates only last item and create references only to this last FirstModel item in loop.

Is there any way to solve this issue?

Thanks.

dxg commented 8 years ago

https://github.com/caolan/async