Data.save() is not a function in Mongoose?

I have

CharData.find({}, {
            characters: {
                $elemMatch: {
                    CharacterID: CharID
                }
            }
        }, (err, data) => {
            if(err) console.log(err)
            if(data[0].characters[0] === undefined) return message.channel.send(`\`\`${CharID}, isn't a valid ID. ex. ${config.prefix}status private/public 0a0aaa00-000a-0aa0-000a-0aa000a0aa00 \`\``)
            data[0].characters[0].status = "Public" 
            data.save().catch(err => console.log(err)) // Returns data.save() is not a function
        })

Anyone know why im getting this error?

I’ve specified most of the stuff in this post:

https://support.glitch.com/t/problem-with-mongodb-findone-returning-null/34562/13

I would have expected the save() method to belong to the CharData object, not to data

But, either way, save is deprecated and you’re better off using updateOne as described here.

Hope it helps

1 Like

Oh, thanks!

Would this work?? @SteGriff

CharData.updateOne({}, {
            characters: {
                $elemMatch: {
                    CharacterID: CharID
                }
            }
        }, function(err, data) {
            if(err) console.log(err)
            if(data[0].characters[0] === undefined) return message.channel.send(`\`\`${CharID}, isn't a valid ID. ex. ${config.prefix}status private/public 0a0aaa00-000a-0aa0-000a-0aa000a0aa00 \`\``)
            data[0].characters[0].status = "Public"
        })

Welp that didn’t work, I got an error and it reset my data lol

Well in the end I just did

CharData.find({}, {
            characters: {
                $elemMatch: {
                    CharacterID: CharID
                }
            }
        }, (err, data) => {
            if(err) console.log(err)
            if(data[0].characters[0] === undefined) return message.channel.send(`\`\`${CharID}, isn't a valid ID. ex. ${config.prefix}status private/public 0a0aaa00-000a-0aa0-000a-0aa000a0aa00 \`\``)
            data[0].characters[0].status = "Public"
            data[0].save().catch(err => console.log(err)).then(message.reply(`\`\`${data[0].characters[0].name}'s status was updated to public\`\``))
        })

How in heck does that happen?
Also, next time you post about mongo make sure to specify Mongoose in the title.

1 Like

Im wondering the same thing lol

@HK420, can I just check… if you look at your packages.json, are you using mongoose or mongodb package (or something else)?

I’m pretty sure it’s Mongoose as capital letters are the hallmark of a mongoose schema.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.