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
x
18
18
1
const Item = require('../models/item_schema');
2
3
const addItem = (req, res) => {
4
let itemData = req.body
5
let itemImage = req.file.path
6
7
Item.create(itemData, itemImage)
8
.then((data) => {
9
if (data) {
10
res.status(201).json(data)
11
}
12
})
13
}
14
15
module.exports = {
16
addItem
17
}
18
item_schema.js
JavaScript
1
42
42
1
const { Schema, model} = require('mongoose')
2
3
const itemSchema = new Schema({
4
title: {
5
type: String,
6
required: [true, 'Title field is required.']
7
},
8
description: {
9
type: String,
10
required: [true, 'Description field is required.']
11
},
12
itemImage: {
13
type: String,
14
required: [true, 'Image field is required.']
15
},
16
userID: {
17
type: Schema.Types.ObjectId,
18
ref: "User",
19
required: [true, 'User field is required']
20
},
21
categoryID: {
22
type: Schema.Types.ObjectId,
23
ref: "Category",
24
required: [true, 'Category field is required']
25
},
26
qualityID: {
27
type: Schema.Types.ObjectId,
28
ref: "Quality",
29
required: [true, 'Quality field is required']
30
},
31
price: {
32
type: Number
33
},
34
claimed:{
35
type: Boolean
36
}
37
}, {
38
timestamps: true
39
})
40
41
module.exports = model('Item', itemSchema)
42
server.js:
JavaScript
1
22
22
1
const express = require('express')
2
const cors = require('cors')
3
const jwt = require('jsonwebtoken')
4
const multer = require('multer');
5
const upload = multer({dest: 'itemImages/'});
6
7
require('dotenv').config()
8
require('./db')()
9
10
const { addItem } = require('./controllers/item_controller')
11
const { loginRequired } = require('./controllers/user_controller')
12
13
const app = express()
14
app.use(cors())
15
app.use(express.json())
16
17
app.post('/items', upload.single('itemImage'), loginRequired, addItem)
18
19
app.listen(port, () => {
20
console.log(`Example app listening on port ${port}`)
21
})
22
Advertisement
Answer
In your item_controller.js try this:
JavaScript
1
19
19
1
const addItem = (req, res) => {
2
let itemData = req.body
3
let itemImage = req.file.path
4
5
Item.create({
6
title: itemData.title,
7
description: description ,
8
itemImage: itemImage ,
9
userID: item.userID ,
10
categoryID: item.categoryID ,
11
qualityID: item.qualityID,
12
//put all data that are required here...
13
}, (err,, data)=> {
14
if (err) return handleError(err) //here you custom handleError of Just res.status(400).json(err);
15
16
res.status(201).json(data)
17
})
18
}
19
Model.create receive as a first params all that property you want to save in your Model and second params it a closure.