Home > OS >  Uploading an image to an API with multer
Uploading an image to an API with multer

Time:04-21

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}`)
})

CodePudding user response:

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.

  • Related