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

Tags: , , , ,



this is the error

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)

this is the code

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() {

this line gives error

let 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");
     
      
      
      
      
      

i have blanked it out

      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();

this fetches 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 => {

// Here we get the data array from the response object

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

// We pass the array to showGiphs function

// We use catch method for Error handling

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

Answer

You’re trying to call split on undefined.

Don’t do that.

First, check to see if it exists.

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

Or use the newer optional chaining.

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


Source: stackoverflow