简体   繁体   English

Nodejs:[ERR_INVALID_ARG_TYPE]:“data”参数必须是string类型或者Buffer的实例等收到Array的实例

[英]Nodejs: [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, etc. Received an instance of Array

i am trying to run this code but it gives me [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.我正在尝试运行此代码,但它给了我 [ERR_INVALID_ARG_TYPE]:“数据”参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例。 Received an instance of Array.收到 Array 的实例。

it runs fine on node.js 12.3.1 but gives this error when trying to run on node.js above v14它在 node.js 12.3.1 上运行良好,但在尝试在高于 v14 的 node.js 上运行时出现此错误

i don't know JavaScript Please help我不知道 JavaScript 请帮忙

this is the error logs ↓↓↓这是错误日志↓↓↓

Online at 07-09-2022   7:32PM
'--------------------------------------
node:internal/fs/utils:890
"  throw new ERR_INVALID_ARG_TYPE(
  ^

�TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Array
)    at Object.writeFile (node:fs:2168:5)
6    at Timeout._onTimeout (/home/node/index.js:92:16)
3    at listOnTimeout (node:internal/timers:564:17)
<    at process.processTimers (node:internal/timers:507:7) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v18.7.0

This is the code where from i getting this error ↓↓↓↓↓↓这是我收到此错误的代码↓↓↓↓↓↓

        // When discord restarts, read from the files
        let timeListBackup = fs.readFileSync('timeListBackup.txt').toString().split(",");
        for(i in timeListBackup) {
            timeList[i] = timeListBackup[i];
        }
        let firstOfflineBackup = fs.readFileSync('firstOfflineBackup.txt').toString().split(",");
        for(i in firstOfflineBackup) {
            firstOffline[i] = firstOfflineBackup[i];
        }

        
            // Backup the values to a text file
            fs.writeFile("timeListBackup.txt", timeList, (err) => {}); 
            fs.writeFile("firstOfflineBackup.txt", firstOffline, (err) => {}); 
        }, 1000);
    });
});

As the error says, you are passing arrays ( timeList and firstOffline ) into writeFile instead of strings, buffers or the like.正如错误所说,您将 arrays ( timeListfirstOffline )传递给writeFile而不是字符串、缓冲区等。 Perhaps you forgot to JSON.stringify or join them.也许您忘记了JSON.stringifyjoin他们。

In fact, looking at how you read the file (with .split(',') ), it seems you want to use join(',') on them:事实上,看看你是如何读取文件的(使用.split(',') ),你似乎想对它们使用join(',')

fs.writeFile("timeListBackup.txt", timeList.join(','), (err) => {});
fs.writeFile("firstOfflineBackup.txt", firstOffline.join(','), (err) => {}); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ERR_INVALID_ARG_TYPE - “from” 参数必须是字符串类型。 接收到一个 Array 实例 - ERR_INVALID_ARG_TYPE - The “from” argument must be of type string. Received an instance of Array TypeError [ERR_INVALID_ARG_TYPE]:“块”参数必须是字符串类型或 Buffer 或 Uint8Array 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array TypeError [ERR_INVALID_ARG_TYPE]:“数据”参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到 Object 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string. Received an instance of Object [ERR_INVALID_ARG_TYPE]:第一个参数必须是字符串类型或缓冲区的实例。 使用 admin.auth().verifyIdToken 时 - [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. when using admin.auth().verifyIdToken TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到未定义和代码:'ERR_INVALID_ARG_TYPE' - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined and code: 'ERR_INVALID_ARG_TYPE' TypeError [ERR_INVALID_ARG_TYPE]:“块”参数必须是字符串或缓冲区类型之一。 接收型号 - TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type number [ERR_INVALID_ARG_TYPE]:“id”参数必须是字符串类型。 接收类型对象 - [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received type object 类型错误 [ERR_INVALID_ARG_TYPE]:“文件”参数必须是字符串类型。 接收类型对象 - TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received type object TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是 string、Buffer、ArrayBuffer、Array 或类似数组的类型之一 Object - TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM