HTTP 事件云函数:请求正文值未定义

编程入门 行业动态 更新时间:2024-10-21 16:05:32
本文介绍了HTTP 事件云函数:请求正文值未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当发送 {"identifiant": "iJXB5E0PsoKq2XrU26q6"} 到下面的云函数时,我无法在请求正文中获取 identifiant 值,它总是会返回 PROBLEMAS NO REQUEST.

When sending {"identifiant": "iJXB5E0PsoKq2XrU26q6"} to the below Cloud Function, I cannot get the identifiant value in the request body and it will always return PROBLEMAS NO REQUEST.

import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; admin.initializeApp(); exports.meusCandidatos = functions.https.onRequest((req, res) => { const identifiant = req.body.identifiant; if(identifiant) res.status(200).json('ok').end(); res.status(500).json('PROBLEMAS NO REQUEST').end(); });

推荐答案

不像 可调用函数,请求体不会自动解析,需要先解析后才能使用.

Unlike a Callable function, the body of a request is not parsed automatically and needs to be parsed before you can use it.

此外,json(...) 将在内部调用 end(),因此您不需要两者.还要确保不要多次调用 end()、send()、json() 等,因为这会导致错误.

In addition, json(...) will call end() internally so you don't need both. Also make sure that you don't call end(), send(), json(), etc. multiple times, as this will lead to errors.

const jsonParser = require('body-parser').json(); exports.meusCandidatos = functions.https.onRequest((req, res) => { jsonParser(req, res, (err) => { if (err) { res.status(500).json({error: err.message}); return; // stop here } const identifiant = req.body.identifiant; if (!identifiant) { res.status(500).json({error: 'PROBLEMAS NO REQUEST'}); return; // stop here } // continue res.status(200).json({ status: 'ok' }); }) });

更多推荐

HTTP 事件云函数:请求正文值未定义

本文发布于:2023-11-28 06:57:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   事件   未定义   正文   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!