3
I intend to store the file in the folder uploads and secure in the bank the name of the file.
The console.log()
show me this:
{theBook: undefined,
cover: undefined}
Stores the image in the folder uploads but don’t file the name in the comic book, coming as Undefined.
'Cause I’m getting this result?:
Typeerror: Cannot read Property 'filename' of Undefined
My file:
eBook.js
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const User = mongoose.model('users');
const requireDir = require('require-dir');
requireDir('../models');
const eBook = mongoose.model('books');
const multer = require('multer');
const uploadConfig = require('../config/upload');
const upload = multer(uploadConfig);
var eBookUpload = upload.fields([
{ name: 'cover', maxCount: 1 },
{ name: 'theBook', maxCount: 1 }
]);
router.post('/add', eBookUpload, async (req, res, next) => {
var erros = []
if(!req.body.title || typeof req.body.title == undefined || req.body.title == null){
erros.push({texto: "Título inválido"})
}
if(!req.body.author || typeof req.body.author == undefined || req.body.author == null){
erros.push({texto: "Autor inválido"})
}
if(erros.length > 0){
res.render("pages/ebooks/create", {erros: erros})
}else{
const filename = req.file.filename;
const newBook = {
title: req.body.title,
author: req.body.author,
genre: req.body.genre,
publisher: req.body.publisher,
edition: req.body.edition,
isbn: req.body.isbn,
pages: req.body.pages,
theBook: filename,
cover: filename,
type: req.body.type,
}
new eBook(newBook).save().then(function(){
req.flash("success_msg", "eBook adicionado com sucesso!")
res.redirect("/ebook/galeria")
console.log(newBook)
}).catch(function(err){
console.log(err)
req.flash("error_msg", "Houve um erro ao salvar o eBook! Tente novamente")
res.redirect("/ebook/galerias")
})
}
});
According to your code the expected
console.log
is not{ theBook: undefined,
 cover: undefined }
whereas you are passing as parameter the constantnewBook
. If I understand correctly, your image is being physically saved but the information onreq.body
are not being filled in and so you cannot save the information of the constantnewBook
in the database?– Marcelo Vismari
No, it’s the other way around... the data from
req.body
are saved in the bank and thereq.file
no. Only store physically in the folder uploads and does not record the file name in the data box in the fields theBook and cover.– Horácio Pedrosa
if I understand correctly the behavior is this. The file is saved physically in the folder uploads. In your database you should create a reference to this file by saving its name, for example. The image bytes will not be saved in the database unless you change your code and adopt a new strategy. You would like to save the image directly in the database?
– Marcelo Vismari
See if this post helps you if I don’t quite understand: https://stackoverflow.com/questions/56359137/how-may-i-upload-file-with-formdata-to-node-js-with-express
– Marcelo Vismari
This is exactly what I would like, but however it does not save the file name in the database. Returns Undefined. You got an idea how to make it better?
– Horácio Pedrosa