I'm trying to use node-fetch to do a GET request. I'm using dotenv to take the url, but the fetch retrieve me TypeError: Only absolute URLs are supported
, the URL_ENV
variable isn't undefined
require('dotenv').config();
let url = process.env.URL_ENV;
fetch(url ,{method: 'GET'}).then(response => {
return response.json()
})
I'm trying to use node-fetch to do a GET request. I'm using dotenv to take the url, but the fetch retrieve me TypeError: Only absolute URLs are supported
, the URL_ENV
variable isn't undefined
require('dotenv').config();
let url = process.env.URL_ENV;
fetch(url ,{method: 'GET'}).then(response => {
return response.json()
})
If I change the url variable with the content of the URL_ENV
inside the .env file it works
let url = "http://192.168.1.140:8080"
fetch(url,{method: 'GET'}).then(response => {
return response.json()
})
Share
Improve this question
asked May 23, 2019 at 12:16
SMattiaSMattia
491 gold badge1 silver badge6 bronze badges
8
- 1 Wele to Stack Overflow! Could you clarify you question? What is not working? What behavior do you expect? – Maris B. Commented May 23, 2019 at 12:22
- Thanks for you interest. I can't understand why putting the same url, in different way, I have two different behaviors. I'm expect that also with the env variable, the first snippet of code, the fetch works. – SMattia Commented May 23, 2019 at 13:53
-
1
Have you tried to check value of
process.env.URL_ENV
at run-time? Try to useConsole.log()
or similar method to find out the actual value. As the error says, it must be an absolute URL. – Maris B. Commented May 24, 2019 at 8:35 -
Yes I have tried,
http://192.168.1.140:8080
is printed – SMattia Commented May 24, 2019 at 9:01 -
3
If the code is not working with
process.env.URL_ENV
and suddenly works when you replace it with string literalhttp://192.168.1.140:8080
then there may be some unnecessary characters like spaces, tabs, newlines, etc.? – Maris B. Commented May 24, 2019 at 9:47
1 Answer
Reset to default 1Do you have mas or semicolon symbols at the end of your lines in .env file? If you do, remove them and it should work
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745633569a4637251.html
评论列表(0条)