我正在尝试使用multer的上载功能来存储从表单输入的图像。但是,每当我尝试使用上载功能时,图像都不会保存,并且来自表单的所有输入都显示为未定义。我已经做了很多谷歌搜索,但是还没有了解导致此问题的原因。当我只使用t upload.single(“ wt ever”)时,一切运行正常。我尝试使用上载功能的原因是为了避免保存图像,即使表单中的输入不正确也是如此。有什么建议?这是指向我的仓库的链接,该仓库具有使用upload.single(“ wt ever):https://github.com/halsheik/RecipeWarehouse.git保存的图像。下面是我与上传功能一起使用的代码。我也乐意接受一些建议,以防止像在仓库中一样,使用upload.single(“ wt ever”)功能保存的图像。
// Modules required to run the application
const express = require('express');
const multer = require('multer');
const crypto = require('crypto');
const path = require('path');
const { ensureAuthenticated } = require('../config/auth');
// Creates 'mini app'
const router = express.Router();
// Models
const Recipe = require('../models/Recipe'); // Recipe Model
// Set up storage engine
const storage = multer.diskStorage({
destination: function(req, file, callback){
callback(null, 'public/uploads');
},
filename: function(req, file, callback){
crypto.pseudoRandomBytes(16, function(err, raw) {
if (err) return callback(err);
callback(null, raw.toString('hex') + path.extname(file.originalname));
});
}
});
const upload = multer({
storage: storage
}).single('recipeImage');
// My Recipes
router.get('/myRecipes', ensureAuthenticated, function(req, res){
Recipe.find({}, function(err, recipes){
if(err){
console.log(err);
} else {
res.render('./home/myRecipes', {
recipes: recipes
});
}
});
});
// My Recipes
router.get('/createRecipe', ensureAuthenticated, function(req, res){
res.render('./home/createRecipe');
});
// Create Recipe
router.post('/createRecipe', ensureAuthenticated, function(req, res){
const { recipeName, recipeDescription, ingredients, directions } = req.body;
let errors = [];
// Checks that all fields are not empty
if(!recipeName || !recipeDescription || !ingredients || !directions){
errors.push({ msg: 'Please fill in all fields.' });
}
// Checks that an image is uploaded
if(!req.file){
errors.push({ msg: 'Please add an image of your recipe' });
}
// Checks for any errors and prevents recipe creation if any
if(errors.length > 0){
// Displays create Recipe form along with errors
res.render('./home/createRecipe', {
errors
});
} else {
// Create a new 'Recipe' using our model
const newRecipe = new Recipe({
recipeName: recipeName,
author: req.user._id,
recipeImageFileName: req.file.filename,
recipeDescription: recipeDescription,
ingredients: ingredients,
directions: directions,
});
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
console.log(err);
} else if (err) {
console.log(err);
}
console.log("SUCCESS");
// Everything went fine.
});
// Saves recipe to mongoDB database
newRecipe.save().then(function(){
res.redirect('/recipes/myRecipes');
}).catch(function(err){
console.log(err);
});
}
});
// Get Single Recipe
router.get('/:id', function(req, res){
// Searches for a 'Recipe' with a unique 'id'
Recipe.findById(req.params.id, function(err, recipe){
if(err){
throw err;
}
// Renders the Recipe in its own page with full information
res.render('./home/recipe.ejs', {
recipe: recipe
});
});
});
// Delete recipe
router.delete('/:id', function(req, res){
const query = {_id: req.params.id}
Recipe.deleteOne(query, function(err){
if(err){
console.log(err);
throw err;
}
res.send('Success');
});
});
module.exports = router;
非常感谢您的帮助。