Reputation: 140
I'am working on a nodejs project that uses gridfs with mongoose. I'm currently trying to upload a file to GridFS but i always have the same error:
TypeError: Cannot read properties of undefined (reading '_id')
at GridFSBucketWriteStream.emitFile (C:\Users\user\app\node_modules\multer-gridfs-storage\src\gridfs.ts:353:12)
at GridFSBucketWriteStream.emit (node:events:530:35)
at GridFSBucketWriteStream.emit (node:domain:488:12)
at finish (node:internal/streams/writable:945:10)
at processTicksAndRejections (node:internal/process/task_queues:82:21)
[nodemon] app crashed - waiting for file changes before starting...
I have followed many tutorials (and github issues) but none of them worked for me.
Connection init.
async function initMongodb() {
mongoose.connect(env.MONGODB_HOSTNAME);
mongoose.connection.once("open", () => {
logger.docs("MongoDB database connection established successfully");
});
let bucket;
mongoose.connection.on("connected", () => {
logger.info("MongoDB connected for GridFSBucket")
bucket = new mongoose.mongo.GridFSBucket(mongoose.connection.db, {
bucketName: "files",
});
});
}
The function that create an storage engine from GridFs
import { GridFsStorage } from "multer-gridfs-storage";
import multer from "multer"
import { env } from "../config/env";
// Create storage engine
export async function upload() {
const mongodbUrl = env.MONGODB_HOSTNAME;
const storage = new GridFsStorage({
url: env.MONGODB_HOSTNAME,
file: (req, file) => {
return new Promise((resolve, reject) => {
const fileInfo = {
filename: "POPO",
bucketName: 'uploads',
mode: "w",
options: {
w: "majority"
}
};
resolve(fileInfo);
});
}
});
return multer({ storage });
}
How i'm calling it:
.... (await upload()).array('attachments'), .....
I have lost hope :(
Upvotes: 0
Views: 113