2

can somebody help me to update some multiple object in loopback but i don't have any idea on how to do it..

this is what i tried...

Bond.ParseBondQoutesheet = (data, cb) => { //eslint-disable-line
    //// now update multiple
    for (let i = 0; i <= data.length; i = +i) {
        const filter = {
            where: { id: data[i].id },
        };
        Bond.findOne(filter, (err, newdata) => {
            if (!err) {
                newdata.updateAttributes(data[i], function (err, updated) {
                    if (!err) {
                        if (data.length === i) {
                            console.log('updated success')
                            cb(null, updated);
                        }
                    } else {
                        console.log('err')
                        console.log(err)
                        cb(err, null);
                    }
                })
            } else {
                cb(err, null);
            }
        });
    }
};

is this correct?

1 Answer 1

1

You can run that but because of JavaScript's async nature it will behave unexpectedly what you can do in order to solve this would be to loop it using recursive method like this

Bond.ParseBondQoutesheet = (data, cb) => { //eslint-disable-line
    //// now update multiple
    let data = data;
    updateAllSync(0);
    function updateAllSync(i) {
        if (i < data.length) {
            const filter = {
                where: { id: data[i].id },
            };

            Bond.findOne(filter, (err, newdata) => {
                if (!err) {
                    newdata.updateAttributes(data[i], function (err, updated) {
                        if (!err) {
                            if (data.length === i) {
                                console.log('updated success')
                                updateAllSync(i+1);
                            }
                        } else {
                            console.log('err')
                            console.log(err)
                            cb(err, null);
                        }
                    })
                } else {
                    cb(err, null);
                }
            });
        }else{
            cb(null,i); // finished updating all docs sync
        }
    }
    };
Sign up to request clarification or add additional context in comments.

4 Comments

you need to make this process run Synchronously other wise it wil behave unexpectedly.. use this code I have provided and you should be fine..
wow great thanks!, but is this the correcy way of updating multiple objects in loopback? sorry i'm newbie here..
well since you need to do this sync way this is the way to do it. because this process need to run one ofter another. there is a method called updateMany() but that's just another story considering your case. if you find this answer was helpful, please mark it as the answer
you can read about it in here but you can't use it in here. because of your execution needs retrieve data from the db and update it . When using updateMany() - mongoosejs.com/docs/api.html#model_Model.updateMany it will only update all the documents matching the same criteria

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.