'Having trouble passing through user data from mongoose inside of a nested promise chain

Having issues with mongoose promise resolve times with Discord.js. I currently have an asynchronous function to fetch a given user and one to add a given user. I know that the fetch gives the proper user object. the first returned promise is not resolving the _points field and is passing through 'undefined' immediately to the next block.

I've tried making sure these both returned promises and changing around the findUser function but nothing seems to have stuck. Any advice on how to make this promise chain work?

On command activation:

try {
    await addUser(userId, userName, guildId, guildName, createdTimeStamp, points)
        .then(() => {
            return new Promise((res, rej) => {
                const _points = findUser(userId, guildId)["points"];
                res(_points);
            });
        })
        .then((response) => {
            return new Promise((res) => {
                console.log(response);
                interaction.editReply({ content: `Current Points: ${response}`, ephemeral: true });
                res('finished');
            });
        });
} catch (e) {
    console.log(`error creating member ${e}`);
}

Mongoose functions:

const findUser = async (userId, guildId) => {
    const member = await EngagementTracking.findOne({ userId: `${userId}`, guildId: `${guildId}` }).exec();
    return await member;
};

const addUser = async (userId, userName, guildId, guildName, createdTimeStamp, points) => { 
    if (findUser(userId, guildId) !== null) return new Promise((res, rej) => {
        res("duplicate member");
    });
    const member = {
        userId: `${userId}`,
        userName: `${userName}`,
        guildId: `${guildId}`,
        guildName: `${guildName}`,
        createdTimestamp: `${createdTimeStamp}`,
        points: points
    }
    return await EngagementTracking.create(member).exec();
};


Solution 1:[1]

@BGPHiJACK was right and sent me down the rabbit hole of finding which promises were executing when. Altered the promise chain to ensure proper execution. For posterity and anyone running into a similar issue:

await addUser(userId, userName, guildId, guildName, createdTimeStamp, points)
    .then((response) => {
        if (response[1]) {
            return new Promise((res) => {
                interaction.editReply({ content: `Current Points: ${response[0]["points"]}`, ephemeral: true });
                res(response[0]);
            });
        } else {
            return new Promise((res) => {
                interaction.editReply({ content: `Now Tracking. Current Points: ${response["points"]}`, ephemeral: true });
                res(response);
            });
        }
    });

And the DB functions:

const findUser = async (userId, guildId) => {
    const member = await EngagementTracking.findOne({ userId: `${userId}`, guildId: `${guildId}` }).exec();
    return member;
};

const addUser = async (userId, userName, guildId, guildName, createdTimeStamp, points) => {
    const userPromise = new Promise((res) => { res(findUser(userId, guildId)); });
    return userPromise.then((response) => {
        if (response !== null) {
            return new Promise((res) =>
            { res([response, "duplicate"]); });
        } else {
            const member = {
                userId: `${userId}`,
                userName: `${userName}`,
                guildId: `${guildId}`,
                guildName: `${guildName}`,
                createdTimestamp: `${createdTimeStamp}`,
                points: points
            };
            return EngagementTracking.create(member);
        }
    });
};

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 tempnameenjoyer