I am working on a NextJS test app, called njs-voice-rcd, where I want to upload some voice recording made by the user to a mongo DB using GridFS. At this point I have the voice recording part already working as expected. But I can't figure out the correct way to upload the audio blob that I have to GridFS. Any relevant tip would by highly appreciated.
This is how I started making the app:
% npx create-next-app@latest
...
% cd njs-voice-rcd
% npm i mongoose gridfs-stream
Beside the above, below is the relevant part of the code for my question:
First, this is the function (stopRecording) executed when I stop the recording process:
const mrRef = useRef<MediaRecorder | null>(null);
.....
const stopRecording = () => {
setRecordingStatus("inactive");
if (!mrRef.current) return
mrRef.current?.stop();
mrRef.current.onstop = () => {
const audioBlob = new Blob(audioChunksRef.current,
{type: mimeType});
const audioUrl = URL.createObjectURL(audioBlob);
setAudio(audioUrl);
loadToMongoDB(audioBlob);
};
}; /* End of stopRecording */
As one can see a function called loadToMongoDB is called to do the work related to my question.
Hereafter is the code of loadToMongoDB. But it does not work the way it is now. Please let me know if you see something wrong.
async function loadToMongoDB(blob:Blob) {
const mongoURI = process.env.NEXT_PUBLIC_MDB_URI_AUDIO!;
try {
await mongoose.connect(mongoURI);
console.log('We have a connection!')
// Work to do once we have a connection.
.....
} catch (error) {
console.error("Error connecting to MongoDB:", error);
}
} /* End of loadToMongoDB */
For the line of code:
await mongoose.connect(mongoURI);
This is the error I get in the web console:
Error connecting to MongoDB: TypeError: mongoose__WEBPACK_IMPORTED_MODULE_2___default().connect is not a function
Here is what I have in the package.json file:
"gridfs-stream": "^1.1.1",
"mongoose": "^7.4.2",
I have also tried some variations for the code inside loadToMongoDB, but nothing is working.
I can provide more detail if necessary. Let me know if this is needed and what I should provide.