【发布时间】:2021-07-15 22:30:12
【问题描述】:
希望你能帮我解决这个问题,对于受过训练的眼睛来说应该很容易! (不是我的情况)
我有这个 Typescript 代码,用作简单的 CloudFunction
export const add2list = functions.https.onRequest((req:any , res:any ) => {
const userId = req.body['UserID'];
const firstName = req.body['first_name'];
const phone = req.body['phone'];
var req1 = http.request(options, function (res1: { on: (arg0: string, arg1: { (chunk: string): void; (): void; }) => void; }) {
var chunks: Array<any>;
res1.on("data", function (chunk?: string) {
chunks.push(chunk);
});
res1.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req1.write(JSON.stringify({
list_ids: [ 'listID' ],
contacts:
[ { country: "test", //phone
email: "test@test.com", //userID
first_name: "Test Testinson", //first_name
custom_fields: {} } ] }));
req1.end();
res.status(200).send("OK");
问题是当我在 req1.write 中使用硬编码数据时,脚本运行良好。当我尝试使用 req 中给出的数据时,它会失败并收到此消息
"TypeError [ERR_INVALID_ARG_TYPE]: "list" 参数必须是 Array 的实例。收到未定义" TypeError:无法读取未定义的属性“推送”
我做错了什么?我很确定有些参数的类型有误。
【问题讨论】:
标签: node.js typescript google-cloud-functions