Reputation: 15
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)
Upvotes: 0
Views: 73
Reputation: 2011
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"
Upvotes: 1