javascript - 如何通过 Promise 链接访问数据(Node-fetch)
问题描述
我想访问从第二次获取返回的用户对象(包含名称、年龄、性别等属性),但每当我这样做时,我都会得到一个意外的对象。我收到 200 条回复,所以我不知道我错过了什么。脚步:
用户登录时获取令牌
使用令牌登录并检索用户数据。
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) })
预期响应:
{
"title": null,
"firstName": "Kay",
"lastName": "Atom",
"primaryPhoneNumber": "+1 712-345-6788",
"fullName": "Kay Atom",
"sex": null,
"email": "kay@bam.com",
"image": null,
}
上面代码中控制台登录的结果:
{
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
指的是响应流。您可能需要类似response.json()
or的东西response.text()
,每个都返回一个承诺。
推荐阅读
- oracle - 本次查询结果的格式化时间
- delphi - Delphi - 泛型类型作为参数
- user-interface - 将 javafx 应用程序的背景从明亮(默认)更改为黑暗
- amazon-web-services - 使用 AWS rekognition 识别图像中的人
- tensorboard - 使用现有的 Tensorboard URL 而不是创建新的实验
- reactjs - 我必须在选择中选择两个值两次,以便它识别我选择的第一个值 - reactjs
- c# - 如果新请求在当前时间跨度之间,则 Rx 缓冲区重置
- python - 如何遍历字符串中的每个字符并将其与字符串中的位置相乘?
- python - 无法在 chrome 操作系统上安装 kivy
- django - 如何在 drf 中使用 uuid 检索数据