Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!