【问题标题】:TypeError [ERR_INVALID_ARG_TYPE] When trying to write err to file in NodejsTypeError [ERR_INVALID_ARG_TYPE] 尝试将错误写入 Nodejs 中的文件时
【发布时间】:2021-08-26 04:59:09
【问题描述】:

所以我正在尝试制作一个错误处理程序,它发送一条消息说“哦,不!有一个未捕获的异常”,其中包含一个包含错误消息的文本文件,但每次我尝试这样做时,它总是这样说:

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received function toString

这是机器人的代码

process.on('uncaughtException', err => {
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require('fs');
const content = err;
const file = '.\errorlog.txt';
const callback = (err) => { 
console.log("The code has failed.")
console.log(err)
setTimeout(wait, 1000); };
console.log("ERROR!!!! D:")
console.log(err)
client.login(no token for you lmao);
client.on('ready', () => {
    console.log("Ready To send Error Log")
    setTimeout(wait, 1000);
    let channel = client.channels.cache.get(I dont feel comfortable sending the channel ID)
    channel.send('Oh No! There was an uncaughtException!');
    fs.appendFile(__dirname + '/textFileName.txt', toString, function (err) {
        if (err) return console.log(err);
        console.log(err, ' > errorlog.txt');
    });
    console.log(err)
    channel.send("Error log", {files: ['./.errorlog.txt']});
    setTimeout(shutdown, 500);
})
})

function shutdown () {
client.destroy();
}

我尝试搜索错误和我的情况,但没有发现任何有用的信息。

我正在使用什么:

  • Nodejs v15.2.1
  • Windows 10 x64
  • npm 7.0.8

谢谢。

【问题讨论】:

  • 您的./.errorlog.txt 不存在或路径错误。并非每次您都将errorlog.txt 作为提供的代码
  • 它确实存在,它实际上是它写入的唯一文件
  • 你走对了吗
  • 对不起,如果这很粗鲁,但我不明白你刚才说的话:|
  • 看我的回答。我的意思是你的文件路径可能是错误的

标签: javascript node.js error-handling discord.js fs


【解决方案1】:

要使用 Discord.js 发送附件文件,您可以执行以下操作:


channel.send("Error log",{
  files: [{
    attachment: 'entire/path/to/file.text', // Make sure file path is right.Can try abosulte path first
    name: 'file.text'
  }]
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2020-12-03
    相关资源
    最近更新 更多