将数据发布到Google Firebase HTTP功能的困难

问题描述 投票:0回答:1

我试图将一些变量作为req.body的一部分发布到Firebase云功能。我使用现代fetch()语法如下:

const { licenseCode } = this.state;
fetch('https://myAPI.com/inputLicense', {
  method: 'POST',
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({ licenseCode })
})

以下是我的Cloud功能的外观:

exports.inputLicense = functions.https.onRequest((request, response) => {
  // const { licenseCode } = request.body
  console.log(request.get('content-type'))
  console.log('query', request.query)
  console.log('body', request.body)
})

不幸的是,所有上述日志记录都会产生空对象,或者在第一行的情况下未定义。在通常的Express设置中,我知道我需要使用:

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

但不知道如何使用云功能。谷歌在他们的文档中声称他们会根据标题自动解析我的请求,但我无法让它工作。

任何帮助都是受欢迎的,谢谢你提前。

node.js express google-cloud-platform google-cloud-functions
1个回答
0
投票

我做了一些测试,显然问题来自对象解构。

以下应该有效:

const obj = { licenseCode: this.state };
fetch('https://myAPI.com/inputLicense', {
  method: 'POST',
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify(obj)
})
© www.soinside.com 2019 - 2024. All rights reserved.