【问题标题】:How to fix undefined error handling node.js如何修复未定义的错误处理 node.js
【发布时间】:2021-11-08 17:17:54
【问题描述】:
const fs = require('fs');
const fileName = './prices.json';
const file = require(fileName);
const price = require('./prices.json');
const fileName = './prices.json';


   if(tmd = message.match(/^!change max (\D+)/)) {
      if(steamID == config.ownerID){
        var kak = tmd[1];
        var sellu = 0;
        sellp = parseInt(message.replace("!change max "+kak,''));
        selli = message.replace("!change max ",'') 
        selli = selli.replace(" "+sellp,'') 
          if (file[selli].max !== undefined){
          file[selli].max = sellp;
          fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
            if (err) return console.log(err);
            client.chatMessage(steamID,"Succesfully changed max of " + selli)
          });
        }else{
        client.chatMessage(steamID,"There was an problem in changing, you might put wrongly name remember about capital letters or\n just do !list and ctrl+c ctrl+v")
        }
    }
  }

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'max' of undefined

由于某种原因不想在它不受保护时跳过错误

【问题讨论】:

    标签: javascript node.js if-statement error-handling undefined


    【解决方案1】:

    您需要先检查文件[selli] 是否未定义。否则你会在你的 if 条件中得到一个错误。

    替换

    if (file[selli].max !== undefined){
    

    对于

    if (typeof file[selli] !== 'undefined' && typeof file[selli].max !== 'undefined' ) {
    

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 2015-04-24
      • 1970-01-01
      相关资源
      最近更新 更多