简体   繁体   中英

How to save array data in nodejs using mongoose?

I am stuck to save data in mongoDb. Here data is in array and i need to insert data if mongodb does not have. Please look code:-

var contactPersonData = [{
    Name: 'Mr. Bah',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'Mr. Sel',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'Mr.ATEL',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'ANISH',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'sunny ji',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'ashish',
    Organization: 'Ashima Limited - Point 2'
}]
console.log('filedata', contactPersonData);
var escapeData = [];
var tempArr = [];

function saveContact(personObj, mainCallback) {
    var tempC = personObj['Organization'].trim();
    var insertData = {};
    Contact.findOne({ companyName: tempC })
        .exec(function(err, contact) {
            if (err)
                return mainCallback(err);
            console.log('find com', contact)
            if (contact) {
                //document exists
                mainCallback(null, insertData);
            } else {
                var newContact = new Contact({ companyName: tempC, createdBy: '58ae5d18ba71d4056f30f7b1' });
                newContact.save(function(err, contact) {
                    if (err)
                        return mainCallback(err);
                    console.log('new contact', contact)
                    insertData.contactId = contact._id;
                    insertData.name = personObj['Name'];
                    insertData.email = personObj['Email'];
                    insertData.contactNumber = { number: personObj['Phone'] };
                    insertData.designation = personObj['Designation'];
                    tempArr.push(insertData);
                    mainCallback(null, insertData);
                })
            }

        });
}
async.map(contactPersonData, saveContact, function(err, result) {
        console.log(err)
        console.log(result)
    },
    function(err) {
        if (err)
            return next(err);
        res.status(200).json({ unsaved: escapeData })

    })

As per above code it has to insert six document instead of one. I think that above iteration not wait to complete previous one. So, the if condition is always false and else is executed.

Your saveContact() function is fine. The reason you get 6 documents instead of 1 document is that async.map() runs you code in parallel. All the 6 requests are made in parallel, not one after the another.

From the async.map() function's documentation -

Note, that since this function applies the iteratee to each item in parallel, there is no guarantee that the iteratee functions will complete in order.

As a result before the document is created in your database all queries are already run and all the 6 queries are not able to find that document since its still in the process of creation. Therefore your saveContact() method creates all the 6 documents.

If you run your code again then no more documents will be formed because by that time your document would be formed.

You should try running your code using async.mapSeries() to process your request serially. Just replace map() with mapSeries() in your above code. This way it will wait for one request to complete and then execute another and as a result only one document will be created. More on async.mapSeries() here .

You seem to be using async.map() wrong.

First, async.map() only has 3 parameters (ie coll , iteratee , and callback ) so why do you have 4? In your case, coll is your contactPersonData , iteratee is your saveContact function, and callback is an anonymous function.

Second, the whole point of using async.map() is to create a new array. You are not using it that way and, instead, are using it more like an async.each() .

Third, you probably should loop through the elements sequentially and not in parallel. Therefore, you should use async.mapSeries() instead of async.map() .

Here's how I would revise/shorten your code:

var contactPersonData = [{
    Name: 'Mr. Bah',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'Mr. Sel',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'Mr.ATEL',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'ANISH',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'sunny ji',
    Organization: 'Ashima Limited - Point 2'
}, {
    Name: 'ashish',
    Organization: 'Ashima Limited - Point 2'
}];

function saveContact(personObj, mainCallback) {
    var tempC = personObj.Organization.trim();
    Contact.findOne({ companyName: tempC }, function (err, contact) {
        if (err)
            return mainCallback(err);
        console.log('found contact', contact);
        // document exists, so mark it as complete and pass the old item
        if (contact) 
            return mainCallback(null, contact);
        // document does not exist, so add it
        contact = new Contact({ companyName: tempC, createdBy: '58ae5d18ba71d4056f30f7b1' });
        contact.save(function (err, contact) {
            if (err)
                return mainCallback(err);
            console.log('created new contact', contact)
            // mark it as complete and pass a new/transformed item
            mainCallback(null, {
                contactId: contact._id,
                name: personObj.Name,
                email: personObj.Email, // ??
                contactNumber: { number: personObj.Phone }, // ??
                designation: personObj.Designation // ??
            });
        });
    });
};

async.mapSeries(contactPersonData, saveContact, function (err, contacts) {
    if (err)
        return next(err);
    // at this point, contacts will have an array of your old and new/transformed items
    console.log('transformed contacts', contacts);
    res.json({ unsaved: contacts });
});

In terms of ?? comments, it means you don't have these properties in your contactPersonData and would therefore be undefined .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM