0

I keep getting an 'x is not a function' error and can't figure out what's causing it.

I've tried attaching the Playlist Manager to the StreamData and it still won't accept it.

The stream data is being returned correctly in the on finish emit except the PlaylistManager is an empty object, and when it gets to the line that calls the PlaylistManager, it says is not a function.

// From The Stream Manager File
PlaylistManager = require('../Playlists/PlaylistManager')
const Streams = new Map()
const addFinishListener = function (StreamData) {
    StreamData.stream.on('finish', async () => {
        console.log("Song Ended. Emitting finish. Asking the PlaylistManager to Play the Next Track...")
        StreamData.PlaylistManager.play_next_track(StreamData)
    })
}
// [Create New Stream]
// Plays the next when stream emites finished.
const createNewStream = function (Broadcast, PlaylistName, track) {
    const stream = yt(track.url, request_options)
    broadcast = Broadcast.play(stream)
    let StreamData = {
        stream,
        track,
        broadcast,
        PlaylistName
    }
    StreamData.PlaylistManager = PlaylistManager

    addFinishListener(StreamData)
    Streams.set(PlaylistName, StreamData)
    return stream;
}

The error :

/// The Output:
Song Ended. Emitting finish. Asking the PlaylistManager to Play the Next Track...
(node:2432) UnhandledPromiseRejectionWarning: TypeError: StreamData.PlaylistManager.play_next_track is not a function
    at PassThrough.<anonymous> (C:\Users\jayca\Documents\Code\MyNextDiscordBot\src\service\broadcast\Streams\StreamManager.js:27:36)
    at PassThrough.emit (events.js:327:22)
    at Object.debug (C:\Users\jayca\Documents\Code\MyNextDiscordBot\src\service\broadcast\Streams\StreamManager.js:52:23)
    at processCommand (C:\Users\jayca\Documents\Code\MyNextDiscordBot\src\service\channel\bot-commands.js:27:27)
    at Object.on (C:\Users\jayca\Documents\Code\MyNextDiscordBot\src\service\channel\bot-commands.js:15:9)
    at C:\Users\jayca\Documents\Code\MyNextDiscordBot\src\service\onMessage.js:11:21
    at Array.forEach (<anonymous>)
    at Client.<anonymous> (C:\Users\jayca\Documents\Code\MyNextDiscordBot\src\service\onMessage.js:9:18)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\jayca\Documents\Code\MyNextDiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
J. Carpenter
  • 91
  • 1
  • 9
  • StreamData.PlaylistManager.play_next_track(StreamData) – J. Carpenter Sep 15 '20 at 03:55
  • I'm calling PlaylistManager.play_next() from the finish emit. The play_next() function could be console.log("hello") and it won't work. It's a valid function. If you really want to see what it all is i can pastebin it but stackoverflow won't let me put any more text in there. I literally pasted te entire StreamManager file. That's all it does. – J. Carpenter Sep 15 '20 at 04:04
  • Because I've imported the file it belongs to, and then called the file.function. What am I missing?? – J. Carpenter Sep 15 '20 at 04:19
  • I don't know ... does `require('../Playlists/PlaylistManager')` define something that has `play_next_track` defined? How would we know, it's your code – Jaromanda X Sep 15 '20 at 04:22
  • `const play_next_track = function (StreamData) { console.log("Hello!") } module.exports = { play_next_track }` – J. Carpenter Sep 15 '20 at 04:27
  • Like I said before. It could be any function. How it is defined doesn't matter. That's what it is. Doesn't matter I just attached the function as a callback and called it once finish was emitted. Thanks. – J. Carpenter Sep 15 '20 at 04:47
  • I've recreated your setup in [this code sandbox](https://codesandbox.io/s/amazing-feather-nxh6x) and it works without problem. The only difference is I defined the `require` statement like `let playlistManager = require(...)`, whilst you have `playlistManager = require(...)` without the `let`. Not using the `let` gave an error so perhaps that could be the problem. – T. Dirks Sep 15 '20 at 08:48

0 Answers0