Reputation: 125
I am using MERN technologies to create a news article web application.
To do this, I have the following Mongoose Schema:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let Article = new Schema({
article_title: {
type: String,
required: true
},
article_summary: {
type: String,
required: true
},
article_content: {
type: String,
required: true
},
article_image: {
type: String,
required: true
},
article_reference: {
type: String,
required: true
}
});
module.exports = mongoose.model('Article', Article);
And the following Express server, where I have a post method that above informations as a "req.body" and "req.file" to save in db using Mongoose and Multer:
const multer = require('multer');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const articleRoutes = express.Router();
const PORT = process.env.PORT || 4000;
let Article = require('./article.model');
app.use(cors());
app.use(bodyParser.json());
mongoose.connect('mongodb://127.0.0.1:27017/article', { useNewUrlParser: true, useUnifiedTopology: true });
const connection = mongoose.connection;
connection.once('open', function() {
console.log("MongoDB database connection established successfully");
});
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
cb(null, Date.now() + "--" + file.originalname);
}
});
const upload = multer({ storage: storage });
articleRoutes.route("/content-management-system/add").post(upload.single("article_image"), (req, res) => {
console.log("START FROM HERE");
const article = new Article({
article_title: req.body.article_title,
article_summary: req.body.article_summary,
article_content: req.body.article_content,
article_image: req.file.article_image,
article_reference: req.body.article_reference
});
console.log(article);
console.log(mongoose.connection.readyState);
article
.save()
.then(() => {
console.log("Saved successfully!");
res.status(200).json({'article': 'article saved successfully'});
})
.catch(err => {
console.log("Article save unsuccessful!");
console.log("article: " + article);
console.log(req.file);
console.log(req.body);
res.status(400).send('adding new article failed');
});
});
However, when I pass a post request on Postman, it fails with a 400 Error saying "Bad Request". As you can see, in the above code, I have many console.logs that shows the value of req.file and req.body and article. When I look on my console, it shows that these values do not include article_image. Below is a photo of the console.logs:
Sorry, for the many code examples, but most of it's just boilerplate code, and the question is simply: Why is my article_image file, which is saving in my image file with Multer, not saving on my MongoDB?
Upvotes: 0
Views: 419
Reputation: 5051
just try article_image: req.file.path
instead of article_image: req.file.article_image
Upvotes: 1