#Array being updated even though it shouldnt

13 messages · Page 1 of 1 (latest)

glad eagle

I know i've had this issue in the past and i was able to solve it, i just dont know how exactly anymore.
this is to update any reason on a case, i know mongodb has a built in function but i cant find anything on it so i just replace the entire array, but both console logs return the same array even though i haven't touched doc.cases.

export const updateReason = async (guild: string, count: number, reason: string) => {
    const coll = db.collection('moderation')
    const q = {guild: guild}
    let doc = await coll.findOne(q)
    if(!doc) return
    let arr: Array<any> = [...doc.cases]
    let el = arr.filter((c : any) => c.count == count)[0]
    let index = arr.indexOf(el)
  
    if(index == -1) return;
    el.reason = reason;
    arr.splice(index, el)
    console.log(doc.cases)
    console.log(arr)

    await coll.updateOne(doc, {
        $set: {
            cases: arr
        }
    })
    
}

ok so codeblocks are broken in posts

pulsar mist
db.collection('moderation').updateOne({
  guild, `cases.${count - 1}`: { $exists: true }
}, { $set: { `cases.{count - 1}.reason`: reason } });

this should work i guess

glad eagle

doesnt work sadly

ok i'm at a loss of words now

i cloned the object with Object.assign

its still changing it

ok JSON.parse(JSON.stringify(doc)) works, i have no fucking clue why js is being such a bitch

weary monolith

javascript i gues

s