簡體   English   中英

如何修復未定義的錯誤處理 node.js

[英]How to fix undefined error handling node.js

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: 無法讀取未定義的屬性“max”

出於某種原因不想在其 undefiend 時跳過錯誤

您需要先檢查 file[selli] 是否未定義。 否則你會在你的 if 條件中得到一個錯誤。

代替

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

為了

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM