Update a user’s schema

Asked

Viewed 29 times

0

Hello! Recently in Discord I made a system of clans, using mongodb atlas, to learn how it works. The problem is that it creates the schema in the right atlas, but gives the update in the user schema.

Here are the two Schemas:

const User = new mongoose.Schema({
    _id: { type: String, required: true },
    tag: { type: String, required: false },
    clan: { type: Boolean, default: false },
    clan_name: { type: String, default: 'The member has no clan.' },
    coins: { type: Number, default: 0 },
    wood: { type: Number, default: 0 },
    rock: { type: Number, default: 0 },
    gold: { type: Number, default: 0 },
});

const Clan = new mongoose.Schema({
    _name: { type: String, required: true },
    all: { type: Array, default: [] },
    owner: { type: String, required: true },
    co_owners: { type: Array, default: 'The clan doesn\'t have co-owners.' },
    members: { type: Array, default: 'The clan doesn\'t have members.' },
    bankLevel: { Type: Number, default: 0 },
    bankGold: { Type: Number, default: 0 },
    bankRock: { Type: Number, default: 0 },
    bankWood: { type: Number, default: 0 },
    members_counter: { type: Number, default: 0 }
});

This is the command code for creating the Clan:

const { Command } = require('discord-akairo');
const { RedEmbed, GreenEmbed } = require("../../Helpers/EmbedHelper");
const Database = require("../../Database");

class CreateClanCommand extends Command {
    constructor() {
        super('createClan', {
            aliases: ['create-clan', 'criarClan', 'criar-clan'],
            category: 'RPG',
            args: [{ id: 'name' }]
        });
    }
    async exec(message, args) {

            await Database.Users.findOne({ '_id': message.author.id }, function (error, document) {

            if (document) {
                if (document.clan === true) {
                    return new RedEmbed('já tens um clan');

                } else {
                    if (document.gold < 500) return new RedEmbed("n tens ouro", message);
                    if (!args.name) return new RedEmbed("põe um nome", message);
                    if (args.name.length > 15) return new RedEmbed("nome com + de 15 caractéres", message);
                    if (args.name.length < 5) return new RedEmbed("nome com - de caractéres", message);

                    let newClan = new Database.Clans({
                        _name: args.name,
                        all: [message.author.id],
                        owner: message.author.id,
                        co_owners: 'The clan doesn\'t have co-owners.',
                        members: 'The clan doesn\'t have members.',
                        bankLevel: 1,
                        bankGold: 0,
                        bankRock: 0,
                        bankWood: 0,
                        members_counter: 1
                    });
                    newClan.save();
                    new RedEmbed('criaste um clan', message)

                    let updateUser = new Database.Users({
                        _id: message.author.id,
                        tag: message.author.tag,
                        clan: true,
                        clan_name: 'The member has no clan.',
                        coins: 0,
                        wood: 0,
                        rock: 0,
                        gold: 0,
                    });
                    updateUser.save();
                }
            } else {
                return new RedEmbed('n tens conta', message);
            }
        });
    }
}

module.exports = CreateClanCommand;

And the mistake talks about a duplicate key, and I have no idea how to fix it: inserir a descrição da imagem aqui

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.