#MongoDB - updateOne()

1 messages · Page 1 of 1 (latest)

alpine storm
#

So I am trying to add data to an already existing element in my mongoDB.

I need help figuring out the correct way to updateOne() of my elements from a collection in my db.

here is the code where I try

async function saveRole(randID, role) {
    console.log(`SAVING ROLE`)
    console.log(`randID: ${randID}`)
    if (!randID || !role) return;
    try {
        await db.collection('interactionData').updateOne(
            { randID: randID },
            { $push: { roles: role } }
        );
        console.log(`saved to db`);
    } catch (error) {
        scripts.logError(error)
        console.log(`ROLE not saved`);
    }
}

In this case db = mongoose.connection
I am getting the result of ROLE not saved and an error of cyclic dependency detected for the await db.collection line.

below is a gist, within it is printed the complete mongoose.connection obj, just in case that might help with finding the correct path to be able to updateOne(). I have had no luck.

https://gist.github.com/Alogantucker/6cd44b84af2845544dc8c005882e2cc7

neon crystal
#

i think you are missing another await on the updateOne function

#

try this instead, i didn't test it yet but it should work

const col = await db.collection('interactionData');
await col.updateOne({ randID }, { $push: { roles: role } });