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')