-4

Everytime adding a new data or updating existing data, new_data and updated_data variables will get increment. But when I try to console log the total count's (new_data and updated_data) at the bottom of the code the result is 0. How do we address this in Node using async ?

Code

let new_data = 0
let updated_data = 0

let vehicles = _.each(results.data, function (value, key) {

    let condition = { VIN: value.VIN }

    Vehicle.model.findOne(condition, function (err, doc) {
        if (doc) {
            let condition2 = { VIN: doc.VIN }
            Vehicle.model.update(condition2, value, function (err, doc1) {
                updated_data += 1
                if (doc && typeof doc.log === 'function') {
                    const data = {
                        action: 'Update',
                        category: 'Inventory Import',
                        message: 'Import Successful',
                        status: '200'
                    }
                    return doc.log(data); 
                }
            })

        } else {
            Vehicle.model.create(value, function (err, doc2) {
                new_data += 1
                if (doc2 && typeof doc2.log === 'function') {
                    const data = {
                        action: 'Create',
                        category: 'Inventory Import',
                        message: 'Import Successful',
                        status: '200'
                    }
                    return doc2.log(data);
                }
            })
        }

    })

})

console.log("new datad : ", new_data)
console.log("updated_data : ", updated_data)
Community
  • 1
  • 1
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – AZ_ Dec 05 '19 at 05:43
  • If you want to do it with `async` you have to access the updated values in the callback or use `Promises`. If you don't want to change any code then you'll have to make the functions non-async. – Leggy Dec 05 '19 at 05:43
  • All i want to do is get the total count of new_data and of updated_data –  Dec 05 '19 at 05:44
  • Yup, it will always return 0, because asynchronous in Javascript show the quickest syntax. Also, you can put your console.log inside the callback function. – Luthfi Dec 05 '19 at 05:53

1 Answers1

1

You can Use ASYNC/AWAIT for that put that code in an async function and then wait for each query for that.I used for of loop because it is synchronous.

const func = async() => { 
    let new_data = 0
    let updated_data = 0

    for(value of results.data){
        try{
        let condition = { VIN: value.VIN }
        let doc = await Vehicle.model.findOne(condition);
        if (doc) {
                let condition2 = { VIN: doc.VIN }
                let doc1 = await Vehicle.model.update(condition2)
                updated_data += 1
                if (doc && typeof doc.log === 'function') {
                        const data = {
                            action: 'Update',
                            category: 'Inventory Import',
                            message: 'Import Successful',
                            status: '200'
                        }
                        doc.log(data); 
                }

            } else {
                let doc2 = await Vehicle.model.create(value);
                    new_data += 1
                    if (doc2 && typeof doc2.log === 'function') {
                        const data = {
                            action: 'Create',
                            category: 'Inventory Import',
                            message: 'Import Successful',
                            status: '200'
                        }
                        doc2.log(data);
                    }
            }
         }catch(err){
            console.log(err);
         }

    }

    console.log("new datad : ", new_data)
    console.log("updated_data : ", updated_data)
}

func();
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Zunnurain Badar
  • 920
  • 2
  • 7
  • 28