0

I am trying to save the id of channels and roles that I have just created so I can go ahead and delete them later on. I am trying to store the IDs in variables so I can later access them publicly from a separate file. This is how I am trying to do it:

let categoryID;
let channelID;
Promise.all([r1, c1, c2]).then(([role, cat, chan]) => {
    chan.setParent(cat);
    chan.overwritePermissions([
            {
                //everyone
                id: '762492106156539945',
                deny: ['SEND_MESSAGES', 'VIEW_CHANNEL']
            },
            {
                //verified
                id: '763048301552992346',
                allow: ['VIEW_CHANNEL']
            },
            {
                //new role
                id: role.id,
                allow: ['SEND_MESSAGES']
            }
        ])
    console.log(chan.id);
    console.log(cat.id);
    channelID = chan.id;
    categoryID = cat.id;
});
console.log(categoryID);
console.log(channelID);

console.log(chan.id) works but console.log(channelID) returns undefined, even though I have set it to chan.id.

  • The two `console.log()` s are executed before the promise resolves and changes the variables. So you will either need to await the `Promise.all().then()` or add the `console.log()`s inside of the then block. Look here for more (this is with callbacks but the points stand): https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron –  Oct 16 '20 at 00:29

1 Answers1

0

I'm not entirely sure, but your problem might've been that you declared the channelID variable after the console log, if you get what I am trying to say. Try this code:

let categoryID;
let channelID;
Promise.all([r1, c1, c2]).then(([role, cat, chan]) => {
    chan.setParent(cat);
    chan.overwritePermissions([
            {
                //everyone
                id: '762492106156539945',
                deny: ['SEND_MESSAGES', 'VIEW_CHANNEL']
            },
            {
                //verified
                id: '763048301552992346',
                allow: ['VIEW_CHANNEL']
            },
            {
                //new role
                id: role.id,
                allow: ['SEND_MESSAGES']
            }
        ])
    channelID = chan.id;
    categoryID = cat.id;
    console.log(chan.id);
    console.log(cat.id);
});
console.log(categoryID);
console.log(channelID);
Irian3x3
  • 56
  • 6