W
W
WapSter2020-09-21 20:49:47
Node.js
WapSter, 2020-09-21 20:49:47

.save() method not working?

The problem is, there is a code

router.post('/add-file-to-play-list', async (req, res) => {
    const user = await User.findById({_id: req.body.userId})
    const music = await Music.findById({_id: req.body.audioId})
    const indexPlayList = await getIndexEl(user.playLists, 'name', req.body.namePlayList)
    const indexMainPlayList = await getIndexEl(music.files, 'name', req.body.nameAudio)
    const playList = user.playLists[indexPlayList]
    if(!playList.files) {
        playList.files = []
    }
    playList.files.push(music.files[indexMainPlayList])
    await user.save()
    res.send(JSON.stringify({
        user
    }))
})

I send a request, in response I receive a generated object
{
    "user": {
        "role": 0,
        "avatar": "storage/avatars/avatar.png",
        "playLists": [
            {
                "name": "Избранные",
                "files": [
                    {
                        "name": "I'm So Sick",
                        "path": "storage/music/flyleaf/01-i'm-so-sick.m4a",
                        "likes": 0,
                        "year": 2005
                    }
                ]
            }
        ],
        "_id": "5f68bf62e85ae5056c5854d8",
        "email": "[email protected]",
        "login": "user",
        "password": "$2a$10$KTiyMuZnC.IYzw81VpqsN.cflWMZJA.dQXGeQhsnWJruvYRKM.00S",
        "__v": 4
    }
}

Ran console.log(user) before await user.save(), the user object matches the changes. Also, the callback function in the save() method works without errors. But there are no changes in the database. The files array should be added.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-09-21
@wapster92

try before saving

user.markModified('playLists')
// или это
// user.markModified('playLists.files')

https://mongoosejs.com/docs/api/document.html#docu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question