Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

324
Visualizações
DJS Cannot read properties of null (reading 'premium')

In my code, before it does anything I have it check if there is data and if there isnt then make a new mongodb collection but when running the command and there is no collection it returns the error Cannot read properties of null (reading 'premium') and then it creates the mongodb collection so when i run it the second time it works, how would i make the collection create first then run the command?

Code:

    run: async (interaction, client) => {

        const data = await guildSchema.findOne({
            id: interaction.guild.id
        });

        if (!data) {
            const newData = new guildSchema({
                id: interaction.guild.id,
                name: interaction.guild.name
            })
            newData.save();
        }

        const types = interaction.options.getString('type');
        const modules = interaction.options.getString('module');
        
        if (types === 'type_enable') {
            if (modules === 'module_all') {

                if (!data.premium) {    
                    await guildSchema.findOneAndUpdate({
                        logChannel: interaction.options.getChannel("channel").id,
                        welcomeChannel: interaction.options.getChannel("channel").id,
                        goodbyeChannel: interaction.options.getChannel("channel").id,
                    })
    
                    await guildSchema.findOneAndUpdate(filter, update, {
                        new: true
                    })

                    interaction.reply('test')
                }
            }
        }
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you're assigning the variable and then creating the doc if it doesn't find a document, you will need to fetch from the database again after creating the new document.

if (!data) {
    const newData = new guildSchema({
        id: interaction.guild.id,
        name: interaction.guild.name
    })
    newData.save();

    data = await guildSchema.findOne({
        id: interaction.guild.id
    });
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda