Issue
I am trying to integrate an API with my angular project which displays the internet speed of my connection. this is an api which is provided by fast.com - https://www.npmjs.com/package/fast-speedtest-api I am trying to follow just as how it is being mentioned in the doc of NPM package. The error that I am receiving is
app.component.ts:18 (intermediate value)(intermediate value)(intermediate value).get is not a function
As per what i understood on google is it must be a syntax error but i dont find any errors in my typescript file. below is my typescript code
const https = require('https');
const http = require('http');
const Timer = require('./Timer');
const ApiError = require('./ApiError');
const FastSpeedtest = require("fast-speedtest-api");
let speedtest = new FastSpeedtest({
token: "YXNkZmFTOKENoYXNkZmhrYWxm", // required
verbose: false, // default: false
timeout: 10000, // default: 5000
https: true, // default: true
urlCount: 5, // default: 5
bufferSize: 8, // default: 8
unit: FastSpeedtest.UNITS.Mbps // default: Bps
});
speedtest.getSpeed().then((s :any) => {
console.log(`Speed: ${s} Mbps`);
}).catch((e :any) => {
console.error(e.message);
});
just in case, i have also checked the async method that was there from the npm package itself. I do not find an error in there as well.
/**
* Get data from the specified URL
*
* @async
* @param {string} url The URL to download from
* @return {Promise} The request and response from the URL
*/
async get(url) {
return new Promise((resolve, reject) => {
const request = (this.https ? https : http).get(url, (response) => {
if (response.headers['content-type'].includes('json')) {
response.setEncoding('utf8');
let rawData = '';
response.on('data', (chunk) => {
rawData += chunk;
});
response.on('end', () => {
const parsedData = JSON.parse(rawData);
response.data = parsedData;
resolve({
response,
request
});
});
} else {
resolve({
response,
request
});
}
}).on('error', (e) => {
reject(e);
});
});
}
what might be the issue?
Solution
fast-speedtest-api is meant to be used on nodejs not an angular app !
Answered By - Matthieu Riegler
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.