花了24小时使它工作,但我仍然无法解决以下问题。
我有一个带有附加应用脚本的google工作表,该工作表从工作表中获取值并将这些值发送到MongoDB Stitch。在应用程序脚本方面:
function getP(){
var msg = {'task':'getproducts','fltrs':{'flt1':'WM', 'flt2':'WM', 'flt3':0};
var mainUrl = 'https://eu-west-1.aws.webhooks.mongodb-stitch.com/api/client/v2.0/app/gsheetsmongodb-sbeab/service/test2/incoming_webhook/webhook0';
var options = {'method':'POST',
//'contentType': 'application/json',
'payload': msg};
var res = UrlFetchApp.fetch(mainUrl, options);
Logger.log(res);
}
现在在MongoDB Stitch上,我有一个脚本可以接收此“有效载荷”,并且应该将其解析为EJSON / JSON格式。
exports = function(payload, response) {
const body = payload.body;
console.log("Request body:", body.text());
console.log("payload query task", payload.query.task);
console.log("payload query content", payload.query.fltrs);
console.log("filter1", payload.query.fltrs.flt1);
}
MongoDB Stitch日志显示:
Logs:
[
"Request body: task=getProducts&fltrs=%7Bfltr1%3DWM%2C+fltr2%3D0.0%2C+fltr3%3DWM%7D",
"payload query task getProducts",
"payload query content {fltr1=WM, fltr3=0.0, fltr2=WM}",
"filter1 undefined"
]
但是如何解析有效载荷的内容?我可以提取payload.query.task,payload.query.fltrs,但无法提取payload.query.fltrs.fltr1的值?
此外,如果我尝试EJSON.parse(payload.query),则会出现以下错误:
Error:
TypeError: invalid character 'f'
Stack Trace:
TypeError: invalid character 'f'
at parse (<native code>)
at apply (<native code>)
at <anonymous>:3:10
at exports (function.js:18:25)
at apply (<native code>)
at function_wrapper.js:3:5
感谢您的帮助和建议,我是JS及其环境的新手