Ayyappa A
Ayyappa A

Reputation: 667

sequelize with async and find method

First apology for my poor english.

I am unable to access the updated vehicle.vsr.selectedvalues outside the find method.

Steps i followed

1) i have a vehicles array and their vsrdetails associated. 2) I am trying to add one more element(contractor_name) in the existing vehicle JSON. 3) For this i am forming new array and send to response.

Impedement is that i am unable to access the updated vehicle.vsr details outside the find method.

     async.forEach(vehicles, function(vehicle, callback){ 
     newvehicles.push(vehicle.selectedValues)
     Contractor.find({ 'where': { 'id': (vehicle.vsr.contractor_id).toString().trim() } }).success(function(contractor){
        console.log(vehicle.vsr.contractor_id +"======"+ contractor.name + "========"+  vehicle.vsr.project_id)
        var vsrnew = Array()
        console.log("***************************************************")
        console.log(newvehicles)
        vehicle.vsr.selectedValues["contractor_name"] = contractor.name
        vsrnew.push(vehicle.vsr.selectedValues)
        newvehicles["vsr"] = vehicle.vsr.selectedValues
      })

     callback() 
   },function(){
      return res.json({ "total": total, "start": start, "limit": limit, "items": newvehicles })
  }) 

Appreciate your patience.

Upvotes: 0

Views: 1581

Answers (1)

clay
clay

Reputation: 6017

Some things I am seeing:

Where are your semicolons?!! : )

Your line newvehicles["vsr"] = vehicle.vsr.selectedValues; is assigning the same value each time. I see you use .push() to add new items to that array at the tope (I think that is your intent from the JSON response property name "items"), and you should do the same at that line. Or assign to a new sub-element.

The variable vehicle is only part of the find() method due to the async.forEach call - the per-item function has a variable named vehicle that can be accessed in that function scope.

Don't create the new Array (var vsrnew = Array()) in the .find() if you want to use it outside of that.

Will gladly comment more, update your question with specifics.

Upvotes: 1

Related Questions