#[solved] Mongoose error on server action

20 messages · Page 1 of 1 (latest)

slender bobcat
#

Hi,
What I am trying to do is upload image on cloudinary and then store that url on mongodb. But wierd thing is happenning. First here is my action file :

"use server";
import { v2 as cloudinary } from "cloudinary";
import Gallery from "@/server/models/Gallery";

cloudinary.config({
  cloud_name: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME,
  api_key: process.env.NEXT_PUBLIC_CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
  secure: true,
});

export const getSignature = async () => {
  const timestamp = Math.round(new Date().getTime() / 1000);

  if (process.env.CLOUDINARY_API_SECRET) {
    const signature = cloudinary.utils.api_sign_request(
      { timestamp, folder: "gallery" },
      process.env.CLOUDINARY_API_SECRET,
    );

    return { timestamp, signature };
  } else {
    throw new Error("CLOUDINARY_API_SECRET is not defined");
  }
};

export const addImageInDB = () => {
  try {
    const newGallery = new Gallery({ title: "hello" });
  } catch (err) {
    console.log(err);
  }
};

and Here is my Gallery Model .

import { Schema, model, models } from "mongoose";
const gallerySchema = new Schema({
  title: {
    type: String,
    required: true,
  },
  photos: [
    {
      type: String,
    },
  ],
});

const Gallery = models.Gallery || model("Gallery", gallerySchema);

export default Gallery;

I am only calling getSignature function from client and not the gallery addImageToDB but while getSignature function is called I guess it also addImageToDB is also being called or initialized I dont know. When i comment it and run it works fine (just function and dont have to comment import).

But when I uncomment it and upload image I get this error :

node_modules/.pnpm/[email protected]/node_modules/mongoose/lib/mongoose.js (102:30) @ new Mongoose
 ⨯ TypeError: Cannot set properties of undefined (setting 'base')
hushed fjordBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

shell mesa
#

When you comment what exactly? The addImageInDB function? I saw you declare it but I dont see you calling it.

slender bobcat
#

addImageInDB is just there. I haven't called it anywhere.

shell mesa
#

Omg how can this be? I mean I dont use Mongoose, but that's too magical for me to understand

#

Do you have a new Gallery statement anywhere else in the app?

shell mesa
#

What if you comment that new Gallery and put a console.log in there, is it really being called?

#

and will the same error occur ?

slender bobcat
#
export const addImageInDB = () => {
  // try {
  //   const newGallery = new Gallery({ title: "hello" });
  // } catch (err) {
  //   console.log(err);
  // }
  console.log("CALLEDDDDDD");
};
#

It dont appear and the app works fine

#

And when I uncomment upper part and then same error

shell mesa
#

Ok, the error is not that this function is being called (because it didn't log). Are you running it with npm run dev or npm run build && npm run start ?

slender bobcat
#

It was causing by
"dev": "next dev --turbo" This little rat

shell mesa
#

Ahá! Great job @slender bobcat

slender bobcat
#

[solved] Mongoose error on server action