Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
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 itemImages\034ed9d34a3b034becdb415d67b7d193.

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

My Code:

item_controller.js:

const Item = require('../models/item_schema');

const addItem = (req, res) => {
    let itemData = req.body
    let itemImage = req.file.path

    Item.create(itemData, itemImage)
        .then((data) => {
            if (data) {
                res.status(201).json(data)
            }
        })
}

module.exports = {
    addItem
}

item_schema.js

const { Schema, model} = require('mongoose')

const itemSchema = new Schema({
    title: {
        type: String,
        required: [true, 'Title field is required.']
    },
    description: {
        type: String,
        required: [true, 'Description field is required.']
    },
    itemImage: {
        type: String,
        required: [true, 'Image field is required.']
    },
    userID: {
        type: Schema.Types.ObjectId,
        ref: "User",
        required: [true, 'User field is required']
    },
    categoryID: {
        type: Schema.Types.ObjectId,
        ref: "Category",
        required: [true, 'Category field is required']
    },
    qualityID: {
        type: Schema.Types.ObjectId,
        ref: "Quality",
        required: [true, 'Quality field is required']
    },
    price: {
        type: Number
    },
    claimed:{
        type: Boolean
    }
}, {
    timestamps: true
})

module.exports = model('Item', itemSchema)

server.js:

const express = require('express')
const cors = require('cors')
const jwt = require('jsonwebtoken')
const multer = require('multer');
const upload = multer({dest: 'itemImages/'});

require('dotenv').config()
require('./db')()

const { addItem } = require('./controllers/item_controller')
const { loginRequired } = require('./controllers/user_controller')

const app = express()
app.use(cors())
app.use(express.json())

app.post('/items', upload.single('itemImage'), loginRequired, addItem)

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your item_controller.js try this:

const addItem =  (req, res) => {
 let itemData = req.body
 let itemImage = req.file.path

 Item.create({
  title: itemData.title,
  description: description ,
  itemImage: itemImage ,
  userID: item.userID ,
  categoryID: item.categoryID ,
  qualityID: item.qualityID,
  //put all data that are required here...
 }, (err,, data)=> {
  if (err) return handleError(err) //here you custom handleError of Just res.status(400).json(err);

  res.status(201).json(data)
 })
}

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!