I want to access the user object(contains attributes like name, age, gender) returned from the second fetch but whenever I do so I get an unexpected object. I get a 200 response so I don't know what I'm missing. steps:
Get the token when the user signs in
Use the token to login and retrieve the user's data.
const fetch = require('node-fetch');
fetch('http://34.245.86.200:9084/api/auth/login', {
method: 'POST',
body: JSON.stringify({
Identifier: '17123456788',
Password: 'bambam'
}),
headers: {
'Content-type': 'application/json'
}
}).then(response => {
return response.json()
}).then(
data => {
const header = {
'Authorization': 'Bearer ' + data.token
}
return fetch('http://34.245.86.200:9085/api/user', {
method: 'GET',
headers: header
})
}).then(msg => {
console.log(msg.body)
})
Expected response:
{
"title": null,
"firstName": "Kay",
"lastName": "Atom",
"primaryPhoneNumber": "+1 712-345-6788",
"fullName": "Kay Atom",
"sex": null,
"email": "kay@bam.com",
"image": null,
}
Results from Console log in code above:
{
token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJrYXlAYmFtLmNvbSIsImVtYWlsIjoia2F5QGJhbS5jb20iLCJnaXZlbl9uYW1lIjoiS2F5Iiwic2lkIjoiMzQ0MDM1ODM5NGZmNGUxNzhkMmJhNzcxNmVjYjM3YTgiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJQYXRpZW50IiwiYXV0aF90aW1lIjoiMDcvMDcvMjAyMCAxNzowOTo1MCIsImV4cCI6MTU5NDE0ODk5MCwiaXNzIjoiTGVpbGFQcm9qZWN0IiwiYXVkIjoiTGVpbGFQcm9qZWN0In0.DHuxUY77Jrsd_wBOrcZ-cmqvE8nh5I3TzpLIrRUuuhI',
message: 'Successfully Logged In kay@bam.com'
}
PassThrough {
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList {
head: [Object],
tail: [Object],
length: 1
},
length: 372,
pipes: [],
flowing: null,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
destroyed: false,
errored: false,
closed: false,
closeEmitted: false,
defaultEncoding: 'utf8',
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: false,
decoder: null,
encoding: null,
[Symbol(kPaused)]: null
},
_events: [Object: null prototype]{
prefinish: [Function: prefinish],
unpipe: [Function: onunpipe],
error: [[Function: onerror], [Function(anonymous)]],
close: [Function: bound onceWrapper]{
listener: [Function: onclose]
},
finish: [Function: bound onceWrapper]{
listener: [Function: onfinish]
}
``
},
_eventsCount: 5,
_maxListeners: undefined,
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: false,
closed: false
},
allowHalfOpen: true,
_transformState: {
afterTransform: [Function: bound afterTransform],
needTransform: true,
transforming: false,
writecb: null,
writechunk: null,
writeencoding: 'buffer'
},
[Symbol(kCapture)]: false
response.body
refers to a stream of the response. You'll likely want something like response.json()
or response.text()
, each which returns a promise.