Skip to content
Advertisement

Uploading an image to an API with multer

I’m trying to make an application that requires image uploading and storing, I’m using Express, Mongoose and Multer and trying to upload to MongoDB.

Currently when I try to upload an image, Multer does create a folder and store the image in that folder, but it doesn’t save the item to the database and it crashes the server giving the error : ObjectParameterError: Parameter "obj" to Document() must be an object, got itemImages34ed9d34a3b034becdb415d67b7d193.

Adding to the database works fine without trying to add the image with multer.

My Code:

item_controller.js:

JavaScript

item_schema.js

JavaScript

server.js:

JavaScript

Advertisement

Answer

In your item_controller.js try this:

JavaScript

Model.create receive as a first params all that property you want to save in your Model and second params it a closure.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement