'TypeError: targetchannel.send is not a function

Node version : v16.13.1

Discord.js version : 13.3.1

TypeError: targetchannel.send is not a function

How to fix this Error?

Code:

fs.readdir('channels', function (err, files) {
if (err) {
    return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
    targetchannel = client.channels.fetch(fs.readFileSync('channels/' + file,{encoding:'utf8'}))
    console.log(targetchannel)
    targetchannel.then((value) => {
            targetchannel.send("asdra")
});

});
});

log of targetchannel = Promise { < pending > }

image of errors https://i.stack.imgur.com/ixfK5.png



Solution 1:[1]

Fix

I was dumb. The bot called .send before logging in.

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
if (!fs.existsSync('channels/')) {
     fs.mkdirSync('channels');
}
fs.readdir('channels', async function (err, files) {
if (err) {
    return console.log('Unable to scan directory: ' + err);
}
files.forEach(async function (file) {
    targetchannel = await client.channels.fetch(fs.readFileSync('channels/' + file,{encoding:'utf8'}))
    console.log(targetchannel)
    targetchannel.send("asdra")

});
});});

Solution 2:[2]

targetChannel is still the pending promise. Use value.send() instead since value is the resolved promise

targetchannel.then((value) => {
    value.send("asdra")
})

Solution 3:[3]

"TypeError: <Channel>.send is not a function" means the channel is not correctly defined or was not found.

In your case you need to await since <Channel>.fetch is an async method.

targetchannel = await client.channels.fetch()

Edit: author fixed it and had another problem

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 MrMythical
Solution 3