简体   繁体   English

使用 node-fetch 发布请求不返回响应

[英]Post request using node-fetch not returning response

I'm new to using node-fetch and catch figure out why I am not getting any response data to print to the console.我是使用 node-fetch 的新手,并弄清楚为什么我没有得到任何响应数据来打印到控制台。

Im using a function to generate firebase user auth tokens for testing but I'm not getting any response whereas in postman my requests with the same data are returning what I want.我使用 function 来生成 firebase 用户身份验证令牌进行测试,但我没有得到任何响应,而在 postman 中,我的请求返回了我想要的相同数据As you can see I've even hard coded the data into the body and still its not returning anything.如您所见,我什至将数据硬编码到正文中,但仍然没有返回任何内容。

async function firebaseSetupAuth(){
  const token1 = admin.auth().createCustomToken("xKdXw06u2tUVE72ETrJVwevCNQo1")
  const token2 = await token1
  token = token2
  const url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=AIzaSyBoKaBKWe9x2LS_n5AHCIEM-5AB_dC99mg"
  const fetchBody = {"token": token,"returnSecureToken":true }
  console.log(JSON.stringify(fetchBody))
  const response1 = fetch(url,{
    method: "post",
    body: fetchBody,
    headers: {'Content-Type': 'application/json'}
  })
  const response2 = await response1
  console.log(response2)
}

You need to return the result of fetch您需要返回fetch的结果

.then((customToken) => {
    return fetch(url,{ // return on this line
      method: "post",
      body: JSON.stringify(/*stuff*/),
      headers: {'Content-Type': 'application/json'}
    }
  )
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM