我的目标是使用Google云功能将发帖请求的结果插入到bigquery中。
我的问题是云函数正在将空值插入到我的表中,因此它无法从发布请求中获取参数。
这是我的帖子请求:
<script>
var event_category = 'action';
var event_name = 'click';
var page_url = 'test'
var request = new XMLHttpRequest();
request.open('POST', 'url to my cloud function');
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
params = {
event_category: event_category,
event_name: event_name,
page_url: page_url
}
request.send( JSON.stringify(params));
console.log(JSON.stringify(params));
</script>
Google云功能的代码
const bigquery = require('@google-cloud/bigquery')();
var result = "";
function insertHandler(err, apiResponse) {
result.writeHead("204",
"No Content",
{
"access-control-allow-origin": "urlofmywebsite",
"access-control-allow-methods": "GET, POST, OPTIONS",
"access-control-allow-headers": "content-type, accept",
"access-control-max-age": 10, // Seconds.
"content-length": 0
}
);
return(result.end());
}
exports.loadBQ = function loadBQ (req, res) {
result = res;
var dataset = bigquery.dataset("google_analytics_test");
var table = dataset.table("google_tag_manager");
var event_category;
var event_name;
var page_url;
try{
event_category = req.body.event_category;
event_name = req.body.event_name;
page_url = req.body.page_url;
var row = {
json: {
event_category : event_category,
event_name: event_name,
page_url: page_url
}
};
var options = {
raw: true
};
table.insert(row, options, insertHandler);
} catch(e){insertHandler(true, false)};
};
如前所述,问题在于从google cloud函数读取发帖请求,不确定如何取消阻止
答案 0 :(得分:1)
代替req.body
使用req.query
,然后像这样通过URL传递变量,以进行HTTP事件触发Google Cloud Function https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/FUNCTION_NAME?event_category=CATEGORY&event_name=EVENT&page_url=URL
当我尝试使用您的代码进行复制时,未分配变量,因此最后将它们分配为undefined
,但是将req.body
修改为req.query
之后,我可以以获得适当的值。
以下是Google Cloud Function的示例代码:
var event_category;
var event_name;
var page_url;
event_category = req.query.event_category;
event_name = req.query.event_name;
page_url = req.query.page_url;
console.log("===VALUES===");
console.log(event_category);
console.log(event_name);
console.log(page_url);
之后,您应该看到在Google Stackdrive日志记录页面中记录的值。
答案 1 :(得分:1)
有一种方法可以处理实际的POST,而不必求助于https://cloud.google.com/functions/docs/writing/http#writing_http_helloworld-nodejs的GET ...
const escapeHtml = require('escape-html');
/**
* Responds to an HTTP request using data from the request body parsed according
* to the "content-type" header.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.helloContent = (req, res) => {
let name;
switch (req.get('content-type')) {
// '{"name":"John"}'
case 'application/json':
name = req.body.name;
break;
// 'John', stored in a Buffer
case 'application/octet-stream':
name = req.body.toString(); // Convert buffer to a string
break;
// 'John'
case 'text/plain':
name = req.body;
break;
// 'name=John' in the body of a POST request (not the URL)
case 'application/x-www-form-urlencoded':
name = req.body.name;
break;
}
res.status(200).send(`Hello ${escapeHtml(name || 'World')}!`);
};