Home > Enterprise >  Displaying Arrays with MongoDB
Displaying Arrays with MongoDB

Time:07-06

Hello StackOverflow people, I require assistance with my Discord bot and my MongoDB database. I've created a schema that has an argument, I guess that is an array. When I find the Schema for my Discord server and try to log the array to the console it sends undefined.

import ConfigSchema from "../../schemas/config.ts";

const configFiles = await ConfigSchema.findOne({
    guildID: message.guild?.id
}).exec();

console.log(configFiles.adminRoleID)
//Should have [ "Test" ] in it, but displays undefined

This is the Schema file

import mongoose from 'mongoose'

let Schema = new mongoose.Schema({
    guildID: String,
    muteRoleID: String,
    modLogChannel: String,
    joinRoleID: String,
    modRoleID: [String],
    adminRoleID: [String],
})

export = mongoose.model('config', Schema)

Thank you!!

CodePudding user response:

const configFiles = await ConfigSchema.findOne({
    guildID: message.guild.id
})//.exec();

console.log(configFiles.adminRoleID) //console.log this first

Then use map or find to get the specific role.

EDIT:

Since you mentioned that the expected output is [ "Test" ], Then you can use configFiles.adminRoleID.length

CodePudding user response:

Try using .then:

await ConfigSchema.findOne({
  guildID: message.guild ? .id
}).then(configFiles => {
  console.log(configFiles.adminRoleID)
});
  • Related