Next.js Discord

Discord Forum

What's wrong with my inventory?

Unanswered
tree🌳 posted this in #help-forum
Open in Discord
So im trying to purchase an item from a marketplace, then have that item added to my inventory. Im working with MongoDB and mongoose. And next-auth too.

Everything was working just fine until i decided that if i buy 2 of the same item separately they should stack and increase quantity. Then this happened. I get an error saying my inventory model is undefined? When it was working before.
Im sending the whole BuyModal.tsx component but im on the handleBuy function. I mean it has no errors but i think there's something wrong with it. Im sending my inventory model too and finally the api/buyitem with the POST req.

Also im getting a bunch of errors related to hydration and of course the undefined inventory. Then there's the terminal with its own thing that i dont know how to fix either. Already reinstalled dependencies and delete node_modules and still the same.

import { Schema, model, Document, Model, models, Types } from "mongoose";
export interface InventoryItem {
  itemIdentifier: string;
  itemName: string;
  rarity: string;
  quantity: number;
  habitat: string[];
  price: number;
  // Add more properties as needed
}

export interface Inventory extends Document {
  userId: Types.ObjectId;
  items: InventoryItem[];
}

const InventorySchema = new Schema<Inventory>({
  userId: {
    type: Schema.Types.ObjectId,
    ref: "User",
    required: true,
  },
  items: {
    type: [
      {
        itemIdentifier: { type: String, required: true },
        itemName: { type: String, required: true },
        rarity: { type: String, required: true },
        quantity: { type: Number, required: true },
        habitat: { type: [String], required: true },
        price: { type: Number, required: true },
        // Add more properties as needed
      },
    ],
    default: [],
  },
});

const InventoryModel: Model<Inventory> =
  models.Inventory || model<Inventory>("Inventory", InventorySchema);

export default InventoryModel;

1 Reply

Also i have to say that most of the code comes from chatgpt and bard because i just have no clue of what im doing wrong anymore. Might go buy food and do chores because im very braindead right now. Hope someone can help