简体   繁体   English

Cloud Function Shell中使用Express App调用HTTP函数时如何传递参数

[英]How to pass parameters when invoking HTTP function using Express App in Cloud Function Shell

This is similar to a previous question of mine这类似于之前的一个问题

I'm testing Cloud Functions in a Firebase Node.js project using Express apps, and don't know how to add query parameters to my test.我正在使用 Express 应用程序在 Firebase Node.js 项目中测试 Cloud Functions,但不知道如何将查询参数添加到我的测试中。

Sample code:示例代码:

const logUUID = (req, res) => {
    console.log("This function is executing!")
    res.send(req.params.uuid)
}

test_app.use("/log_uuid", logUUID)
exports.test = functions.https.onRequest(test_app)

I am calling it through this:我通过这个调用它:

test.get("/logParams")

Which does indeed log这确实记录

This function is executing!这个功能正在执行!

I don't know how to pass in a 'uuid' query parameter, even after reading the firebase docs and the request readme linked in the firebase docs.即使在阅读了 firebase 文档firebase 文档中链接的请求自述文件之后,我也不知道如何传入“uuid”查询参数。 I've tried everything I could come up with:我已经尝试了我能想到的一切:

test.get("/logParams?uuid=1234")
test.get("/logParams",{uuid:1234})
...

How can I do this?我怎样才能做到这一点?

This can be done with a 'qs' argument:这可以通过“qs”参数来完成:

    test.get("/logParams", qs: {uuid:"1234"})

Or including the query string in the path itself或者在路径本身中包含查询字符串

    test.get("/logParams?uuid=1234")

The problem in your code is that instead of req.params.uuid you should use req.query.uuid在你的代码的问题是,不是req.params.uuid你应该使用req.query.uuid

See the difference between req.params and req.query查看req.paramsreq.query 的区别

暂无
暂无

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

相关问题 了解 express 应用程序是否作为 Firebase 云功能运行 - Know if express app is running as firebase cloud function 当Express应用位于函数内部时,如何在Node.js中模拟Express? - How to mock express in nodejs when express app is located inside function? 如何使用node js express将变量传递到函数[不同文件]中 - How to pass variable into function [ different file] using node js express 如何在 Google Cloud Functions 中调用 Express function? - How to call an Express function in Google Cloud Functions? 如何在不调用 function 的情况下将参数传递给 function? - How to pass parameters to function without calling function? 如何按路由将参数传递给通用Express中间件功能? - How can I pass parameters to a generic Express middleware function on a per-route basis? 如何在 React Native 中使用上下文 API + Hooks 传递 function 的参数 - How to pass parameters of a function using Context API + Hooks in React Native Mongo shell 方法在 express 应用程序中抛出“不是函数”错误 - Mongo shell methods throwing "not a function" error in express app 如何在Google Cloud Puppeteer函数内部传递两个参数? 解析以请求JSON - How to pass two parameters inside a google cloud puppeteer function? Resolve to req to JSON 当 Node.js Express 应用程序以 Lambda Function 由 serverless-http 包装时运行时的路由问题? - Routing problems when Node.js Express app runs as Lambda Function wrapped by serverless-http?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM