TypeError: Cannot read property 'split' of undefined in discord and nodejs

TypeError: Cannot read property 'split' of undefined in discord and nodejs

这是错误

let token = msg.content.split(' ');
                          ^

TypeError: Cannot read property 'split' of undefined
    at Client.msg (/app/bot.js:15:27)
    at Client.emit (events.js:196:13)
    at MessageCreateAction.handle (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/discord.js/12.5.1/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/ws/7.4.2/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:196:13)
    at Receiver.receiverOnMessage (/rbd/pnpm-volume/3c4dae99-ae7d-482d-8189-d37183b015d2/node_modules/.registry.npmjs.org/ws/7.4.2/node_modules/ws/lib/websocket.js:825:20)

这是代码

console.log("loding");
const Discord = require("discord.js");
const client = new Discord.Client();

client.login(process.env.SECRET);

client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on("message", msg);

    function msg() {

这一行报错

让token = msg.content.split(' ');

  if ((msg.channel.id = "791583092275937303")) {
    if (token[0] === "ping") {
      msg.reply("Pong!");
    } else if (token[0] === "!gif") {
      msg.reply("ok wait");
     
      
      
      
      
      

我把它删掉了

      let APIKEY = "**************";
      let limit = "100";
      let search= 'ok';
      let rating = 'g'
      if (token.length > 1){
        search.slice(1,search.length).join(" ");
      }
      
      const gifForm = document.querySelector("#gif-form");
      gifForm.addEventListener("submit", fetchGiphs);

      function fetchGiphs(e) {
        e.preventDefault();

这会获取 giphy gif

        fetch(
          `https://api.giphy.com/v1/gifs/search?&q=${search}&limit=${limit}&api_key=${APIKEY}&rating=${rating}`
        )
          .then(response => {
            return response.json();
          })
          .then(resp => {

// 这里我们从响应对象中获取数据数组

let dataArray = resp.data;
            console.log(dataArray);
            msg.channel.send()
})        

// 我们将数组传递给 showGiphs 函数

// 我们使用 catch 方法来处理错误

 .catch(err => console.log(err)); 
      }
    }
  }
}

您正试图在 undefined 上呼叫 split

不要那样做。

首先,检查它是否存在。

let token;
if (msg && msg.content) {
  token = msg.content.split(" ");
}

或者使用较新的可选链接。

let token = msg?.content?.split(' ');