[solved] Mongoose error on server action
Answered
Tan posted this in #help-forum
TanOP
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 :
and Here is my Gallery Model .
I am only calling
But when I uncomment it and upload image I get this error :
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/mongoose@8.0.3/node_modules/mongoose/lib/mongoose.js (102:30) @ new Mongoose
⨯ TypeError: Cannot set properties of undefined (setting 'base')
17 Replies
When you comment what exactly? The addImageInDB function? I saw you declare it but I dont see you calling it.
TanOP
Yes I haven't called it. In client side when uploading the image I call
getSignature
function that's it. When I comment addImageInDB
it works fine.addImageInDB
is just there. I haven't called it anywhere.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?TanOP
nope !!
I also dont understand
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 ?
TanOP
I will try
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
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
?TanOP
Okay I found the bug
Answer
TanOP
It was causing by
"dev": "next dev --turbo"
This little ratAhá! Great job @Tan