简体   繁体   中英

I'm trying to map an array inside another map function my code is not running synchronously

i have an array of lead details(which i need to insert into db) and each lead is having a field named interestArea which is an array of string, I have to find each interestArea and if found the interestArea from the db, i will have to store the ObjectId of the interestArea or else i will have to create the new interestArea in db and store its objectIds in interestArea as field of the lead

My input

let jsonArray = [ { firstName: 'newLead1',
                    lastName: 'newLead1',
                    company: 'hub',
                    companySize: '22',
                    designation: 'software Enginner',
                    phoneNumber: '1234567890',
                    jobRole: 'engineer',
                    email: 'newLead1@hub.com',
                    leadSource: 'online',
                    industry: 'it',
                    location: 'ernakulam',
                    annualTurnOver: '10',
                    requestedMeetingDate: '2019-03-28T09:13:02.958Z',
                    interestArea: 'app,newspaper' },

                    { firstName: 'newLead2',
                      lastName: 'Sharma',
                      company: 'hub',
                      companySize: '20',
                      designation: 'software Enginner',
                      phoneNumber: '1234567891',
                      jobRole: 'engineer',
                      email: 'newLead2@hub.com',
                      leadSource: 'web',
                      industry: 'software',
                      location: 'kacherippady',
                      annualTurnOver: '15',
                      requestedMeetingDate: '2019-03-28T09:13:02.958Z',
                      interestArea: 'website,newspaper' } ]

Below is the function i have tried

function jsonArrayMap(jsonArray){
  return new Promise(function(resolve,reject){
      let validateInFn = [];
      (async ()=>{
        validateInFn = await jsonArray.map(lead => {
          (async ()=>{
            console.log("step: 1");
            console.log("interestArea is present",lead.interestArea);
            let interestAreaArray = lead.interestArea.split(',');
            console.log("interestAreaArray",interestAreaArray);
            let interestAreaObjectIdArrayInFn = await interestAreaArray.map(interestArea => {
              console.log("step: 2");
              console.log("each word interestArea",interestArea)
              InterestArea.find({where:{name:interestArea}},(err,interestAreaInDb)=>{
                console.log("step: 3");
                console.log("search in db got interestAreaInDb",interestAreaInDb);
                if(interestAreaInDb.length>0) {
                  console.log("step: 4");
                  return interestAreaInDb[0].id
                }
                else {
                  console.log("step: 4");
                  InterestArea.create({name:interestArea},(err,newInterestArea)=>{
                    return newInterestArea.id
                  });
                } //else
              }) //find is there an interestArea with this name
            }) //interestAreaArray.map
            console.log("interestAreaObjectIdArrayInFn",interestAreaObjectIdArrayInFn)
          })();
        })
        console.log("validateInFn",validateInFn)
      })();
    }) //promise
  } //jsonArrayMap

Output was

step: 1
interestArea is present app,newspaper
interestAreaArray [ 'app', 'newspaper' ]
step: 2
each word interestArea app
step: 2
each word interestArea newspaper
step: 1
interestArea is present website,newspaper
interestAreaArray [ 'website', 'newspaper' ]
step: 2
each word interestArea website
step: 2
each word interestArea newspaper
interestAreaObjectIdArrayInFn [ undefined, undefined ]
interestAreaObjectIdArrayInFn [ undefined, undefined ]
validateInFn [ undefined, undefined ]
step: 3
search in db got interestAreaInDb [ { name: 'app', id: 5ca799d2491c0cd8f415f980 } ]
step: 4
(node:56798) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
step: 3
search in db got interestAreaInDb []
step: 4
step: 3
search in db got interestAreaInDb [ { name: 'website', id: 5ca799d2491c0cd8f415f981 } ]
step: 4
step: 3
search in db got interestAreaInDb []
step: 4

I was expecting the code to await at places where i have used it but it didn't worked as i expected

I have also tried using the async npm

i have used their forEachOf function that also didn't worked

Below is the function i have tried

function jsonArrayMap(jsonArray){
      return new Promise(function(resolve,reject){
          let validateInFn = [];
          async.forEachOf(jsonArray, (lead, key, callbackforEachOfjsonArray) => {
            console.log("step: 1");
            console.log("interestArea is present",lead.interestArea);
            let interestAreaArray = lead.interestArea.split(',');
            console.log("interestAreaArray",interestAreaArray);
            let interestAreaObjectIdArrayInFn = [];

            async.forEachOf(interestAreaArray, (interestArea, key2, callbackforEachOfInterestAreaArray) => {
              console.log("step: 2");
              console.log("each word interestArea",interestArea)
              InterestArea.find({where:{name:interestArea}},(err,interestAreaInDb)=>{
                console.log("step: 3");
                console.log("search in db got interestAreaInDb",interestAreaInDb);
                if(interestAreaInDb.length>0) {
                  console.log("step: 4");
                  interestAreaObjectIdArrayInFn.push(interestAreaInDb[0].id)
                  if(interestAreaArray.length -1 == key2) callbackforEachOfInterestAreaArray()
                }
                else {
                  console.log("step: 4");
                  InterestArea.create({name:interestArea},(err,newInterestArea)=>{
                    interestAreaObjectIdArrayInFn.push(newInterestArea.id)
                    if(interestAreaArray.length -1 == key2) callbackforEachOfInterestAreaArray()
                  });
                } //else
              }) //find is there an interestArea with this name
            }, function (err,result2) {
              console.log("result2",result2)
              if(jsonArray.length -1 == key) callbackforEachOfjsonArray()
          })
        },function (err,result) {
          console.log("result",result)
        })
        }) //promise
      } //jsonArrayMap

Output was

step: 1
interestArea is present app,newspaper
interestAreaArray [ 'app', 'newspaper' ]
step: 2
each word interestArea app
step: 2
each word interestArea newspaper
step: 1
interestArea is present website,newspaper
interestAreaArray [ 'website', 'newspaper' ]
step: 2
each word interestArea website
step: 2
each word interestArea newspaper
step: 3
search in db got interestAreaInDb [ { name: 'app', id: 5ca799d2491c0cd8f415f980 } ]
step: 4
(node:56939) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
step: 3
search in db got interestAreaInDb [ { name: 'newspaper', id: 5ca894c3aa7a64dddefa2674 } ]
step: 4
step: 3
search in db got interestAreaInDb [ { name: 'website', id: 5ca799d2491c0cd8f415f981 } ]
step: 4
step: 3
search in db got interestAreaInDb [ { name: 'newspaper', id: 5ca894c3aa7a64dddefa2674 } ]
step: 4

There are a few things wrong in your code:

  1. You construct a new Promise although you don't have to; just construct one if you have a callback that you wrap.

  2. You do call two functions that take a callback, but you don't wrap them in a new Promise .

  3. You await on the result of .map ; that is always a no op as .map returns an array and await only works on Promises. If you have an array of promises, you can use Promise.all and turn it into a promise resolving to an array; then you can await that.


Now, your code can't be easily fixed. So let's start from scratch.

At first, let's wrap the callbacks into promises:

const findInterestArea = (name) => 
  new Promise((resolve, reject) => InterestArea.find({where: { name }}, (err, result)=> err ? reject(err) : resolve(result));

const createInterestArea = (name) => 
  new Promise((resolve, reject) => InterestArea.create({ name }, (err, result) => err ? reject(err) : resolve(result));

Now, the next step of abstraction would be a findOrInsert operation:

async function findOrCreateInterest(name) {
  const exists = await findInterestArea(name);
  if (exists) return exists;
  return await createInterestArea(name);
}

Now you can easily iterate over the jsonArray and each interestArea and insert it:

async function jsonArrayMap(jsonArray) {
  for(const lead of jsonArray) {
    const interests = lead.interestArea.split(',');
    for (const interest of interests) {
      const { id } = await findorCreateInterest(interest);
      // do stuff with id
    }
  }
}
  async function jsonArrayMap(jsonArray) {
    let leads = [];
    let newInterestArrayOfObjectIds = [];
   for(const lead of jsonArray) {
      const interests = lead.interestArea.split(',');
      for(const interest of interests) {
        const { id } = await findorCreateInterest(interest);
        newInterestArrayOfObjectIds=id;
      }
      leads.push({
                  firstName:lead.firstName,lastName:lead.lastName,
                  company:lead.company,companySize:Number(lead.companySize.replace(/,/g,'')),
                  designation:lead.designation,phoneNumber:lead.phoneNumber,
                  jobRole:lead.jobRole,email:lead.email,
                  leadSource:lead.leadSource,industry:lead.industry,
                  location:lead.location,annualTurnOver:Number(lead.annualTurnOver.replace(/,/g,'')),
                  interestArea:(lead.interestArea)? newInterestArrayOfObjectIds : [],
                  reqestedMeetingDate:(lead.requestedMeetingDate)? new Date(lead.requestedMeetingDate) : undefined,
                  csvTracker:csvTracker
                  })
   }
   return leads
}

async function createLead () =>{
  let leads = await jsonArrayMap(jsonArray);

    let mat = await LeadCollection.aggregate([
      //i have to do some aggregations here
    ]).toArray();
    //then only i can insert lead here
}

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