smartcar / node-sdk

Smartcar Node.js SDK
MIT License
50 stars 14 forks source link

smartcar.Vehicle(vehicle_id, access_token) returns undefined after update #19

Closed RobinJayaswal closed 7 years ago

RobinJayaswal commented 7 years ago

The following code worked to return vehicle objects before the update:

return Promise.map(vehicle_ids.vehicles, function(vehicle_id) {
     var vehicle = client.getVehicle(access.access_token, vehicle_id);
     return vehicle.info();
});

I see from the README this is no longer the way to do it, and changed the code to:

console.log(vehicle_ids);
console.log(access.access_token);
return Promise.map(vehicle_ids.vehicles, function(vehicle_id) {
   var vehicle = smartcar.Vehicle(vehicle_id, access.access_token);
   console.log(vehicle)
   return vehicle.info();
});

However, vehicle is now undefined, even right after I go through the OAuth process and have a completely new access token in hand.

{ vehicles:
   [ '0b909b28-3b01-489c-8580-88e8d4555bd7',
     '2fc302a3-cf2d-4a24-8b42-d0a3f2789625',
     'aa4cb9ab-e388-441d-937a-8f24a0bb6851' ],
  paging: { count: 3, offset: 0 } }
cf036cf8-e675-4510-87f1-a8742cf1c9f7
TypeError: Cannot read property 'info' of undefined
  at /mnt/c/Users/robinjayaswal/Documents/GitHub/iotUIClient/serverless_site/functions/dashboard_render.js:70:23
  at bound (domain.js:280:14)
  at runBound (domain.js:293:12)
  at tryCatcher (/mnt/c/Users/robinjayaswal/Documents/GitHub/iotUIClient/serverless_site/node_modules/bluebird/js/release/util.js:16:23)
ramya-s commented 7 years ago

missing the 'new' keyword. Try:

var vehicle = new smartcar.Vehicle(vehicle_id, access.access_token);

gurpreetatwal commented 7 years ago

constructors should throw error if not called with new

function Vehicle(...) {
  if (!(this instanceof Vehicle)  throw Error()
}