如何通过Promise链访问数据(节点获取)

发布时间:2020-07-07 16:55

我想访问第二次获取返回的用户对象(包含名称,年龄,性别等属性),但是每当这样做时,我都会得到一个意外对象。我收到200条回复,所以我不知道自己缺少什么。步骤:

  1. 用户登录时获取令牌

  2. 使用令牌登录并检索用户数据。

    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
回答1

response.body是指响应流。您可能想要response.json()response.text()之类的东西,每个东西都返回一个承诺。