#E11000 duplicate key error collection

9 messages · Page 1 of 1 (latest)

cyan streamBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
hidden stream

inventory.js:

const { Schema, model } = require('mongoose');
const fs = require('fs');
const path = require('path');
const boatCapacityFilePath = path.join(__dirname, '../Data/boatCapacityDefaults.json');
const boatCapacities = JSON.parse(fs.readFileSync(boatCapacityFilePath, 'utf8'));

const inventoryItemSchema = new Schema({
    item_id: { type: String, required: true },
    name: { type: String, required: true, minlength: 3, maxlength: 100 },
    description: { type: String, maxlength: 1000 },
    is_usable: { type: Boolean, default: false },
    is_sellable: { type: Boolean, default: true },
    quantity: { type: Number, required: true },
    capacity: { type: Number, required: true },
  });
  
  const boatInventoryItemSchema = new Schema({
    item_id: { type: String, required: true },
    name: { type: String, required: true, minlength: 3, maxlength: 100 },
    description: { type: String, maxlength: 1000 },
    is_usable: { type: Boolean, default: false },
    quantity: { type: Number, required: true },
    capacity: { type: Number, required: true },
  });

  const inventorySchema = new Schema({
    userID: { type: String, required: true, unique: true},
    items: [inventoryItemSchema],
    capacity: { type: Number },
    // Добавьте дополнительные поля по необходимости
  });
  
const boatInventorySchema = new Schema({
    userID: { type: String, required: true, unique: true},
    boatName: { type: String, required: true},
    boatEngraving: { type: String, required: true},
    items: [boatInventoryItemSchema],
    capacity: { type: Number },
  });

  boatInventorySchema.pre('save', function (next) {
    if (this.isNew && this.boatName && boatCapacities[this.boatName]) {
      this.capacity = boatCapacities[this.boatName]
    }
  next();
  })

  const InventoryItem = model('InventoryItem', inventoryItemSchema);
  const BoatInventoryItem = model('BoatInventoryItem', boatInventoryItemSchema);
  const Inventory = model('Inventory', inventorySchema);
  const BoatInventory = model('BoatInventory', boatInventorySchema);

  module.exports = { InventoryItem, BoatInventoryItem, Inventory, BoatInventory };```

buy-boat.js:

async execute(interaction) {
      const userID = interaction.user.id;
      const boat = interaction.options.getString('boat');
      const engraving = interaction.options.getString('engraving');
  
      try {
        if (!userID) {
          interaction.reply('Неверный идентификатор пользователя.');
          return;
        }
  
        const existingBoat = await BoatInventory.findOne({ userID });
  
        if (existingBoat) {
          interaction.reply('У вас уже есть корабль. Вы не можете иметь более одного корабля.');
          return; 
        }

        if (userID === null) {
          interaction.reply('Неверный идентификатор пользователя.');
          return;
        }
  
        const newBoatInventory = new BoatInventory({
          userID: userID,
          boatName: boat,
          boatEngraving: engraving,
          items: [], 
        });
  
        await newBoatInventory.save();
        const imagePath = path.join(__dirname, '../../Images/image.png');
        interaction.reply({ content: `**Клабаутерман взял гравировальные инструменты, и нанес на "${boat}" гравировку "${engraving}"**`, files: [imagePath] });
      } catch (error) {
        console.error(error);
        interaction.reply('Ошибка при сохранении информации о корабле.');
      }
    },
  };

Commands only work for me

simple sparrow

Why does your error show it as userId but your code has userID everwhereThonk

hidden stream

I have a question. What happens if I delete all Collections through the site?