Answer the question
In order to leave comments, you need to log in
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)
})
}
})
Answer the question
In order to leave comments, you need to log in
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()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question