D
D
dfhkjhg2020-06-29 23:49:55
JavaScript
dfhkjhg, 2020-06-29 23:49:55

Why can't I save the modified mongoose object?

const dbObjects = await Model.find({ b })

    for (const obj of parsedObjects) {
        const dbObject = dbObjects.find(elem => elem.id === obj.id)

        if(!dbObject) {
            //dbObjects.shift(obj)
        } else if(!(dbObject.e.length === obj.e.length)){
            dbObject.e = obj.e
            console.log(dbObjects)
        }
    }

    await dbObjects.save()


Why does dbObjects.save is not a function give an error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-06-30
@dfhkjhg

.find() will return an array even if there is only one document, and an empty array if none is found, and an array cannot be .save()

for (const obj of parsedObjects) {
        const dbObject = dbObjects.find(elem => elem.id === obj.id)
         .....
        // а вот здесь наверно получится
        await dbObject.save()
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question