Search code examples
node.jsalexa-skills-kit

Unable to parseFloat a string with double apix


I'm trying to use parseFloat function to get float value from a string, the string is returned from an API call and it's value looks like this "1.1"

But when i'm casting the parseFloat(string) it returns NaN..

Here is my code:

    const response = await httpGet(); // response here returns "1.1"
    let importo = parseFloat(response.toString()); // return NaN
    console.log(importo)

Here is the httpGet:

  return new Promise(((resolve, reject) => {
    var options = {
        host: 'www.example.cloud',
        port: 443,
        path: '/api/alexa',
        method: 'GET',
    };
    
    const request = https.request(options, (response) => {
      let returnData = '';
      response.on('data', (chunk) => {
        returnData += chunk;
      });

      response.on('end', () => {
        console.log(returnData)
        resolve(returnData);
      });

      response.on('error', (error) => {
          console.log(error)
          reject(error);
      });
    });
    request.end();
  }));

And here is a console log of the value and the importo

enter image description here

I'm using Alexa Developer Console to do so...


Solution

  • Actually i was able to solve the issue only by passing from my API an object instead of a simple return of the string as in any case i was unable to use parseFloat.

    Then by returning an object with the correct data type (float) all works as it have to without any need of use of parseFloat.