T
T
TipoImya2020-08-22 10:21:39
JavaScript
TipoImya, 2020-08-22 10:21:39

Why is data not being stored in MongoDB?

const Family = require('./data/family.js');

bot.on("message", async message => {
  if(message.content.startsWith("!faminvite")) {
    const user = message.mentions.members.first();
    const args = message.content.split(/ +/);
    if(!args[1]) {
      return await message.channel.send(`<@${message.author.id}>, вы не указали название семьи!`)
    }
    if(!args[2]) {
      return await message.channel.send(`<@${message.author.id}>, вы не указали пользователя, которого хотите пригласить!`)
    }
    if(!user) {
      return await message.channel.send(`<@${message.author.id}>, вы не правильно указали пользователя.`)
    }
    Family.findOne({FamilyName: args[1]}, async(err, fam) => {
      if(err) console.log(err);
      if(!fam) {
        return await message.channel.send(`\`<@${message.author.id}>, такой семьи нету!\``)
      }
      if(fam.FamilyInfo.FamilyMembers.includes(user.id) && fam.FamilyInfo.FamilyMembersDescr.includes(`<@${user.id}>`)) {
        return await message.channel.send(`\`<@${message.author.id}>, пользователь <@${user.id}> уже состоит в вашей семье!\``)
      }
      await fam.FamilyInfo.FamilyMembers.push(user.id)
      await fam.FamilyInfo.FamilyMembersDescr.push(`<@${user.id}>`)
      await fam.save().catch(err => console.log(err))
      console.log(fam.FamilyInfo.FamilyMembersDescr)
      console.log(fam.FamilyInfo.FamilyMembers)
    })
  }
})

For some reason, the data is not being written to the database, what could be the problem?

DB:
5f40c73915e5a033011314.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-08-22
@TipoImya

maybe due to a bunch of nested objects mongoose/mongo doesn't understand that the original object has changed, try

// не надо await добавление в массив
fam.FamilyInfo.FamilyMembers.push(user.id)   
fam.FamilyInfo.FamilyMembersDescr.push(`<@${user.id}>`)

fam.markModified('FamilyInfo')
// возможно придется сообщать об изменении каждого вложенного массива
// fam.markModified('FamilyInfo.FamilyMembers')   // ?

await fam.save()

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