0

I am trying to execute a function that should return true, however I continue to get a return of undefined and I am not sure why.

filterVehicle(myOBJ.availableVehicleList, myOBJ.paymentType, myOBJ.selectedVehicle, myOBJ.maxLimit, myOBJ.selectedVehicleAmt, function (response, selectedCar) {    
    var promotableCar = processResponse(response, selectedCar);
    if(promotableCar){
        return true;
    }
});

The response looks something like this (array of objects):

[{
  make: chevy,
  model: camaro
}, {
  make: chevy,
  model: tahoe
}, {
  make: chevy,
  model: suburban
}]

processResponse then basically widdles the array down to 1 vehicle/object

When I alert(promotableCar) it returns [object, object]

If I alert(promotableCar.make) it returns the make of the object returned by processResponse (e.g. chevy)

Since technically promotable car is technically available/present, using the if(promotableCar) should return true.

What am I doing wrong? I can't seem to figure it out.

Thanks in advance for any assistance.

Renan Araújo
  • 3,533
  • 11
  • 39
  • 49
user2828701
  • 307
  • 1
  • 3
  • 18
  • Please see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992), which this duplicates. – Dave Newton Jan 14 '20 at 18:37
  • You've got a callback function inside filterVehicle. How are you using the cb in the original function? – anna Jan 14 '20 at 18:42
  • It sounds like the inner function that looks like `function (response, selectedCar) { ... }` is working as expected - but the final result of `undefined` would be coming from the outermost function, `filterVehicle`. – Gershom Maes Jan 14 '20 at 18:43
  • processResponse is returning a falsy value – Rainb Jan 14 '20 at 18:45
  • so, what is your `processResponse` function doing? In case promotableCar isn't truthy you don't do anything with it from what you have shared, so it's hard to know what is really happening – Icepickle Jan 14 '20 at 18:45
  • I think that the close reason might not be apt, since he doesn't do any assignment of the result of the function call, to be fair, I think it should be closed, but just as unclear as there are to many unknown in this snippet – Icepickle Jan 14 '20 at 18:47
  • I think filterValue both filters and returns a callback with response. The callback is probably processing it correctly. However, he does not mention where he is trying to check for the true value. I think he's trying to read the filterValue's result for a true value. If he console.logged within promotableCar's if statement, then it should be already true. If filterValue was a promise, he should await filterValue's result. – SILENT Jan 14 '20 at 18:53
  • @Icepickle the processResponse function either returns null or returns a single object – user2828701 Jan 14 '20 at 19:09
  • @SILENT the filterVehicle function returns an object { make: chevy, model: suburban}. if i write promotableCar.make, I get the correct value (chevy), however in addition to getting the correct value, I also want the filterVehicle function to return true (I have another dependent function that needs to see that filterVehicle returned as true) – user2828701 Jan 14 '20 at 19:16
  • By stating filterVehicle returns a car object, you mean something like `const result = filterVehicle(... ` where the `result` is `{ make: chevy, model: suburban }`? Does it return undefined or null if there are no results? If so, `if (result) { ... follow up function }` – SILENT Jan 14 '20 at 19:24

0 Answers0