0

Im testing out a way to get the user name of a specific discord user id and add it to a database. The problem im having is that the program is returning the promise rather than the value after I use the client.users.fetch() function from discordjs. I set await in front of it so that the program can wait for me to get the data, but it not only logs undefined to the console, im unable to add it to the Schema due to it returning a promise. I feel like i've tried everything at this point including async/await. Any information helps.

This is the code for the get_user function:

const get_user = async (id, message) => {
    const resp = await client.users.fetch(id)
        .then((user) => {
            console.log(user.username);
            message.channel.send(`Username: ${user.username}`);
            return user.username;
        })
        .catch((err) => {
            console.log("FOUND ERROR: " + err);
        });
    console.log("resp " + resp);
    return resp;
}

This is the code for the function that calls get_user:

const handle_update = (id1, id2, fav, value, message) => {
    DisUser.findOne({disID: id1})
        .then((result) => {
            if(result === null){
                console.log("cant find disUser");
                const userName = get_user(id1, message);
                console.log('username: ' + userName);
                const disUserData = {
                    disID: id1,
                    userName: userName,
                    owed: [],
                    done: [],
                    num: 100
                };
                disUserData.done.push(fav);
                create_disUser(disUserData);
            }

And this is the output of the console

errors: {
    userName: CastError: Cast to string failed for value "Promise { <pending> }" (type Promise) at path "userName"
        at SchemaString.cast (C:\Users\Ultim\Documents\Code\JSProjects\goodLooksBot\node_modules\mongoose\lib\schema\string.js:603:11)
        at SchemaString.SchemaType.applySetters (C:\Users\Ultim\Documents\Code\JSProjects\goodLooksBot\node_modules\mongoose\lib\schematype.js:1189:12)
        at model.$set (C:\Users\Ultim\Documents\Code\JSProjects\goodLooksBot\node_modules\mongoose\lib\document.js:1403:20)
        at model.$set (C:\Users\Ultim\Documents\Code\JSProjects\goodLooksBot\node_modules\mongoose\lib\document.js:1137:16)
        at model.Document (C:\Users\Ultim\Documents\Code\JSProjects\goodLooksBot\node_modules\mongoose\lib\document.js:161:12)
        at model.Model (C:\Users\Ultim\Documents\Code\JSProjects\goodLooksBot\node_modules\mongoose\lib\model.js:115:12)
        at new model (C:\Users\Ultim\Documents\Code\JSProjects\goodLooksBot\node_modules\mongoose\lib\model.js:4827:15)
        at create_disUser (C:\Users\Ultim\Documents\Code\JSProjects\goodLooksBot\main.js:22:22)
        at C:\Users\Ultim\Documents\Code\JSProjects\goodLooksBot\main.js:61:17
        at processTicksAndRejections (node:internal/process/task_queues:96:5) {
      stringValue: '"Promise { <pending> }"',
      messageFormat: undefined,
      kind: 'string',
      value: [Promise],
      path: 'userName',
      reason: null,
      valueType: 'Promise'
    }
  },
  _message: 'DisUser validation failed'
}

0 Answers0