【问题标题】:TypeError [ERR_INVALID_ARG_TYPE]: The "list" argument must be an instance of Array. Received undefinedTypeError [ERR_INVALID_ARG_TYPE]:“list”参数必须是 Array 的实例。收到未定义
【发布时间】: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


    【解决方案1】:

    Chunck 越来越不确定,chunck 需要的数组是可选的试试这个,让我们看看

    res1.on("data", function (chunk?: string) {
         if(chunck)
          chunks.push(chunk);
        });
    

    【讨论】:

    • 嗨,@Syed 它不起作用,显示同样的错误!
    • 尝试在res1.on(data)里面添加console.log(chunck),看看chunck是否未定义
    • 感谢@Syed 块未定义,与代码无关!
    • 是的,试着看看你会从那里知道的错误不断增长兄弟
    猜你喜欢
    • 2020-09-16
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2021-09-08
    • 2020-12-30
    • 1970-01-01
    • 2019-10-01
    相关资源
    最近更新 更多