报错但仍能正常运行

Posted

技术标签:

【中文标题】报错但仍能正常运行【英文标题】:Gives an error but still runs properly 【发布时间】:2018-09-02 13:56:01 【问题描述】:

在我从另一位用户那里得到同一行代码的帮助后,一个新的“错误”浮出水面。即使记录了错误,它仍然可以找到使我的代码正常工作的方法。如果有解决方案,请告诉我,但如果没有,请解释原因。我希望这种情况不会发生,即使它可能不会导致任何问题,但最好是这样我可以阅读更重要的错误,这些错误会导致我的代码出现问题。这是我的代码:

const Discord = require('discord.js');
const botconfig = require("./botconfig.json");
const fs = require("fs");
const bot = new Discord.Client();
bot.on("message", async message => 
    let prefix = botconfig.prefix;
    let messageArray = message.content.split(" ");
    let cmd = messageArray[0];
    let args = messageArray.slice(1);
    console.log(message.member.id)
    var playerFile = JSON.parse(fs.readFileSync(`./playerData/$message.member.id.json`, "utf8"));
    if (cmd === `$prefixfc`) 
        fs.exists(`./playerData/$message.member.id.json`, function(exists) 
            if (exists) 
                let ar = args[0];
                let ninConsole = args[1];
                let code = args[2];
                if (ar === "add" || ar === "remove") 
                    if (code) 
                        if (ar === "add") 
                            console.log("Add");
                            if (ninConsole === "switch") 
                                console.log("Switch " + code);
                                let fileContent = `"switch": "$code","threeDS": "$playerFile.threeDS"`
                                fs.writeFile(`./playerData/$message.member.id.json`, fileContent, (err) => 
                                    if (err) 
                                        console.error(err);
                                        return;
                                    ;
                                    message.delete();
                                );
                            
                            if (ninConsole === "3ds") 
                                let fileContent = `"switch": "$playerFile.switch","threeDS": "$code"`
                                fs.writeFile(`./playerData/$message.member.id.json`, fileContent, (err) => 
                                    if (err) 
                                        console.error(err);
                                        return;
                                    ;
                                    message.delete();
                                );
                            
                         else if (ar === "remove") 
                            if (ninConsole === "switch") 
                                let fileContent = `"switch": "None","threeDS": "$playerFile.threeDS"`
                                fs.writeFile(`./playerData/$message.member.id.json`, fileContent, (err) => 
                                    if (err) 
                                        console.error(err);


                         return;
                                ;
                                message.delete();
                            );
                        
                        if (ninConsole === "3ds") 
                            let fileContent = `"switch": "$playerFile.switch","threeDS": "None"`
                            fs.writeFile(`./playerData/$message.member.id.json`, fileContent, (err) => 
                                if (err) 
                                    console.error(err);
                                    return;
                                ;
                                message.delete();
                            );
                        
                    
                
             else 
                console.log('Got to here');
                let codeEmbed = new Discord.RichEmbed()
                    .setTitle(`$message.author.username's FriendCodes`)
                    .setColor('#FDB25F')
                    .addField('Switch Code', playerFile.switch, true)
                    .addField('3DS Code', playerFile.threeDS, true)
                    .setFooter('Deleting in 30 seconds...')
                message.channel.send(codeEmbed)
                    .then(msg => 
                        msg.delete(30000)
                    );

            
         else 
            return message.delete();
        
    );

// ...

这是错误:

(node:27903) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open './playerData/425674570851418113.json'
    at Object.fs.openSync (fs.js:646:18)
    at Object.fs.readFileSync (fs.js:551:33)
    at Client.bot.on (/home/yello/Desktop/Bot/index.js:45:40)
    at emitOne (events.js:116:13)
    at Client.emit (events.js:211:7)
    at MessageCreateHandler.handle (/home/yello/Desktop/Bot/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (/home/yello/Desktop/Bot/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (/home/yello/Desktop/Bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (/home/yello/Desktop/Bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
    at WebSocket.onMessage (/home/yello/Desktop/Bot/node_modules/ws/lib/event-target.js:120:16)
(node:27903) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:27903) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

您正在尝试获取不存在文件的内容。你需要检查一个文件是否存在,如果它没有创建它并分配一个默认值 我是@AndréPaulo。当它说fs.exists(``./playerData/$message.member.id.json``, function(exists) if(exists) 但在您读取文件并尝试解析它之前。 【参考方案1】:

您的顺序有误。 您正在尝试解析文件,然后检查文件是否存在:

console.log(message.member.id)
var playerFile = JSON.parse(fs.readFileSync(`./playerData/$message.member.id.json`, "utf8"));
if (cmd === `$prefixfc`) 
    fs.exists(`./playerData/$message.member.id.json`, function(exists) 
        if (exists) 
            let ar = args[0];

但是你需要先检查文件是否存在,然后读取并解析它。所以它会是这样的:

console.log(message.member.id)
if (cmd === `$prefixfc`) 
    fs.exists(`./playerData/$message.member.id.json`, function(exists) 
        if (exists) 
            var playerFile = JSON.parse(fs.readFileSync(`./playerData/$message.member.id.json`, "utf8"));
            let ar = args[0];

【讨论】:

以上是关于报错但仍能正常运行的主要内容,如果未能解决你的问题,请参考以下文章

C# 没有报错但窗体不出现?

转译时排除 *.spec.ts 文件,但仍能正确 lint

Eclipse导入Web项目后代码不报错但项目报错(左上角有红叉)解决方案

Flutter App Build apk报错但运行正确解决

为啥有时候python编译器没报错但运行了一点就终止了

Visual Studio 编译正常,但仍显示红线