0

I'm trying to integrate a database into my DiscordBot app and currently having a problem with inputting new user into MongoDB using Mongoose. The console also show no err at all.

const mongoose = require('mongoose')
const User = require('../models/userModels')

module.exports = message => {
    User.findOne({userID:message.author.id}, (err,user)=>{
        if(user == null){ //Create new db collections if none was found
            const user = new User({
                _id: mongoose.Types.ObjectId(),
                userID: message.author.id,
                username: message.author.username,
                role: ["default"],
                balance: 0,
                level: 1,
                exp: 0,
                lastAttendance: message.createdAt
            })

            user.save()
            .then(result => {
                console.log(result)
                message.reply("Information saved")
            })
            .catch(err => console.log.err)

        }
}

This is my model code:

const mongoose = require('mongoose')
const Schema = mongoose.Schema

var userSchema = new Schema({
    _id: Schema.Types.ObjectId,
    userID:{type: String, unique:true, required:true},
    username: String,
    balance:{type: Number, require:true},
    role: {type:String, enum: ['default','admin']},
    level:{type: Number, require:true},
    exp:{type: Number, require:true},
    lastAttendance: Date
})

module.exports = mongoose.model('Users', userSchema)
1
  • could you console.log(user) before the if condition, to check if the user exists or not? also console.log(message.author) to check its contents Commented Jun 12, 2020 at 11:42

1 Answer 1

1

The reason why you are not getting error logs is because of the console.log syntax in the catch statement is wrong. Change console.log.err to console.log(err)

The type defined for role in the userSchema is String and you are passing it as array role: ["default"]. You need to change this to role: "default".

Also on success, if you are trying to set the key reply to the message object with value "Information saved" then you should replace message.reply("Information saved") with message.reply = "Information saved"

Sign up to request clarification or add additional context in comments.

1 Comment

Problem solved, thanks!! By the way, message.reply was a function from discord.js module, so I'm ok with that part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.