【发布时间】:2020-06-02 15:49:48
【问题描述】:
我已经成功部署了一个谷歌云函数,它接收来自 POST 请求的参数。我现在正在尝试将其更改为从 GET 请求中获取参数,因为参数不包含任何私有数据。
似乎我正确地获取了传入的参数,但是当我尝试将它们传递给 bigQuery 时,它告诉我我的查询缺少参数。我知道我的代码是正确的,因为如果我对参数的值进行硬编码,它可以正常工作,例如:
bigQuery.createQueryJob({
query,
params: {
"make": "acura",
"model": "mdx",
"modelYear": 2005
}
}).then...
我也知道我得到了正确的参数,因为如果我将我的云函数更改为只返回传入的查询字符串参数,它会正确返回它们(下面注释掉的行)。如果我将云功能更改为使用 req.body 而不是 req.query 并使其成为 POST 请求,它也可以正常工作。
我不知道为什么“参数”没有正确传递给 createQueryJob。任何帮助将非常感激。这是代码(出于隐私原因,我不得不删除实际查询):
package.json:
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"@google-cloud/bigquery": "^2.0.6"
}
}
index.js:
const { BigQuery } = require("@google-cloud/bigquery");
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.getRecallDataByVehicleInfo = (req, res) => {
res.set('Access-Control-Allow-Origin', "*");
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader(
"Access-Control-Allow-Headers",
"X-Requested-With,content-type"
);
const params = req.query;
// res.status(200).send("make is - " + params.make + ", model is - " + params.model + ", model year is -" + params.modelYear);
// return;
const bigQuery = new BigQuery();
const query = `myQuery
where Make = @make
and Model = @model
and ModelYear = @modelYear`
bigQuery.createQueryJob({
query,
params
}).then(results => {
const job = results[0];
return job.getQueryResults({
autoPaginate: false,
timeoutMs: 1000000
},
callback());
});
const callback = () => (err, rows) => {
if (err) {
res.status(401).send(JSON.stringify(err));
}
else {
res.status(200).send(rows);
}
};
}
【问题讨论】:
-
如果您的问题有更新,您可以使用底部的编辑链接对其进行编辑。无需添加评论。
-
@MykWillis 感谢您的回复!我不确定你是什么意思。你可能想参考这个cloud.google.com/bigquery/docs/parameterized-queries
-
@PaulFabbroni 啊,很明显!
标签: node.js google-bigquery google-cloud-functions