Hello can someone explain to me why
https://api.github.com/users/donjon1234#
this link is the api in the browser show an response object flat.
but when i try use it in node js like this,
the same link or endpoint nothing works
and console log only shows status 200 ok
etx but where is the actual object response?
We can’t see that code that you have in front of you, you’re going to have to post the relevant bit or we can’t really help.
loadJson('https://api.github.com/users/donjon1234');
function loadJson(url){
return fetch(url)
.then(response=>{
if(response.status==200){
response.json();
console.log(response.login);}
else{
console.log('error')
}
})
}
the object
{
"login": "donjon1234",
"id": 35605339,
"node_id": "MDQ6VXNlcjM1NjA1MzM5",
"avatar_url": "https://avatars3.githubusercontent.com/u/35605339?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/donjon1234",
"html_url": "https://github.com/donjon1234",
"followers_url": "https://api.github.com/users/donjon1234/followers",
"following_url": "https://api.github.com/users/donjon1234/following{/other_user}",
"gists_url": "https://api.github.com/users/donjon1234/gists{/gist_id}",
"starred_url": "https://api.github.com/users/donjon1234/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/donjon1234/subscriptions",
"organizations_url": "https://api.github.com/users/donjon1234/orgs",
"repos_url": "https://api.github.com/users/donjon1234/repos",
"events_url": "https://api.github.com/users/donjon1234/events{/privacy}",
"received_events_url": "https://api.github.com/users/donjon1234/received_events",
"type": "User",
"site_admin": false,
"name": null,
"company": null,
"blog": "",
"location": null,
"email": null,
"hireable": true,
"bio": null,
"public_repos": 1,
"public_gists": 1,
"followers": 0,
"following": 0,
"created_at": "2018-01-19T15:43:01Z",
"updated_at": "2018-05-28T14:10:13Z"
}
the object in node js look very different
What does it look like in nodejs. I would expect it to be logging ‘donjon1234’. Note you aren’t returning anything on success either, just console logging a value
That’s not the response object. That’s the error object. There are 3 misconceptions here
-
Fetch ONLY errors out with a network error
- https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful
- this means you need to check for
response.ok === true
- this also means that you’d still receive a valid response from the server, so you have to deal with that on the receiving end.
-
fetch doesn’t work in node, so you must be using a 3rd party library?
- the problem may be a difference in their api with the browsers fetch method
-
You are not returning anything from your fetch call. So the result you see in node is actually 3 things if you look at the lines right after your command
node json.js
-
{}
-> this is your result from callingconsole.log(response.login)
-
undefined
-> this is the result of your function because you’re not returning anything in yourthen
method. -
[ Body ....
-> this looks to be the result of the fetch call made by the 3rd party library.
-
The easiest way to tell is to label your console statements like
console.log('this is the json resp: ', json)
// which outputs
this is the json resp: {..some json object }
well ye its a library fetch ofc.
I solved the problem allready…
I did the promise wrong. so instead of just like i did code it suppose to be
fetch(url).
then(result => resutlt.json()).
then etx
But thanks for help i dont know what response i got but it seems like the ?headers
and not the body.
or its the error object like u said i have no clue.
i just made an own api and called it with frontend just simple
but then its twitter module in node js.
Res.json() returns another promise.
You have to write:
.then(res => res.json())
.then(data => console.log(data))
Just to clarify, this works because you are returning a value
fetch(url).
// arrow func implicitly returns a single expression (no curly braces)
then(result => resutlt.json()).
then etx
While here you weren’t
function loadJson(url){
return fetch(url)
.then(response=>{
if(response.status==200){
// not returning anything here
// should be 'return response.json()'
response.json();
console.log(response.login);}
else{
console.log('error')
}
})
}
And you should still check for a successful response since fetch will only error for a network connection issue, and I’m assuming your using node-fetch.
They maintain a parallel api to window.fetch. So anything besides a network issue will give you a response object.
fetch(url).
then(result => {
// since we used braces we must explicitly return
if (result.ok) return result.json()
else // handle bad result, e.g: result.ok === false, result.status === 400
})
.catch(e => {
// only for connection issues, e.g: network down, url malformed
})
ye i was thinking about that too.
why it failed if in {} and passed without curly braces.
seems alot tobe clueless about yet.
however but i guess its a question of keeping track of what functions return a promise??
fetch(url).
// arrow func implicitly returns a single expression (no curly braces)
then(result => resutlt.json()).
then etx
This is because {}
curly braces form a block statement in javascript. When the javascript parser sees curly braces, it thinks you want to group statements together. Therefore you need to tell it to return something.
Think of it like this —
// fat arrow means the function returns something
=>
// So, this is saying
// "the function someFunc returns 3"
const someFunc = () => 3
// you could also write it like a function declaration,
// but we have to tell the function what to return
// because of the curly braces
function someFunc() {
return 3
}
// that then means that this is saying
// "the function someFunc returns a block statement"
const someFunc = () => {}
// but because we're returning a block, we have to
// tell the function what to return
const someFunc = () => {
return 3
}
The same principle applies when you use an arrow in a then clause
// fetch the url
fetch(url)
// then return result.json()
.then(result => result.json())
// fetch the url
fetch(url)
// then return a block statement
.then(result => {
result.json()
})
// so we have to return something explicitly
// fetch the url
fetch(url)
// then return a block statement
.then(result => {
// which returns result.json()
return result.json()
})
Hopefully that clears it up some. Let me know if there’s something I didn’t make clear.