我正在开发一个股票图像网站,并面临使用cloudinar和multer将图像上传到cloudinary的问题。 “”添加图片功能有效,但添加个人资料图片功能无效。有人可以帮我这个忙吗? 我的代码在下面的屏幕截图中。我正在使用node.js + Express进行开发。 非常感谢!
//======================================================================
//CLOUDINARY + MULTER SETUP
//======================================================================
var multer = require('multer');
var storage = multer.diskStorage({
filename: function(req, file, callback) {
callback(null, Date.now() + file.originalname);
}
});
var imageFilter = function (req, file, cb) {
// accept image files only
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/i)) {
return cb(new Error('Only image files are allowed!'), false);
}
cb(null, true);
};
var upload = multer({ storage: storage, fileFilter: imageFilter})
var cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: xxxxxxx ,
api_key: xxxxxxx,
api_secret: 'xxxxxxx
});
下面给出的代码
//CREATING NEW IMAGE
app.post("/images", isLoggedIn, upload.single('image'),function(req, res){
cloudinary.uploader.upload(req.file.path, function(result) {
// add cloudinary url for the image to the campground object under image property
var image=req.body.image;
image = result.secure_url;
var title= req.body.title;
var price=req.body.price;
var author={
id: req.user._id,
username: req.user.username,
profile_picture: req.user.profile_picture
};
var description=req.body.description;
var newImage = {title: title, image: image, description: description, price: price,author: author};
Image.create(newImage, function(err, newlyCreated){
if(err){
console.log(err);
}else{
console.log(newlyCreated);
res.redirect("/images");
}
});
});
});
这就是我要解决的问题
//Adding Profile Picture FORM
app.get("/profile/:id/edit", function(req, res){
User.findById(req.params.id, function(err, foundUser){
if(err){
req.flash("error", "Oops Something went wrong! Try again Later!");
res.redirect("back");
}else{
console.log(foundUser._id);
res.render("userDisplayPicture", {user: foundUser});
}
});
});
app.put("/profile/:id", upload.single('image'), function(req, res){
cloudinary.uploader.upload(req.file.path, function(result) {
// add cloudinary url for the image to the campground object under image property
var profile_picture = req.body.profile_picture;
profile_picture = result.secure_url;
User.findByIdAndUpdate(req.params.id, profile_picture, function(err, foundUser){
if(err){
req.body.flash("error", "Oops! Something went wrong! Try again later!");
res.redirect("/profile/"+ req.params.id);
}
else{
req.body.flash("success", "Profile Picture Updated Successfully!");
res.redirect("/profile/"+req.params.id);
}
});
});
});
错误
错误:意外字段 在makeError(/home/ubuntu/workspace/StockImage/v7/node_modules/multer/lib/make-error.js:12:13) 在wrappedFileFilter(/home/ubuntu/workspace/StockImage/v7/node_modules/multer/index.js:40:19) 在Busboy。 (/home/ubuntu/workspace/StockImage/v7/node_modules/multer/lib/make-middleware.js:114:7) 在emitMany(events.js:127:13) 在Busboy.emit(events.js:201:7) 在Busboy.emit(/home/ubuntu/workspace/StockImage/v7/node_modules/busboy/lib/main.js:38:33) 在PartStream。 (/home/ubuntu/workspace/StockImage/v7/node_modules/busboy/lib/types/multipart.js:213:13) 在emitOne(events.js:96:13) 在PartStream.emit(events.js:188:7) 在HeaderParser中。 (/home/ubuntu/workspace/StockImage/v7/node_modules/dicer/lib/Dicer.js:51:16) 在emitOne(events.js:96:13) 在HeaderParser.emit(events.js:188:7) 在HeaderParser._finish(/home/ubuntu/workspace/StockImage/v7/node_modules/dicer/lib/HeaderParser.js:68:8) 在SBMH。 (/home/ubuntu/workspace/StockImage/v7/node_modules/dicer/lib/HeaderParser.js:40:12) 在emitMany(events.js:127:13) 在SBMH.emit(events.js:201:7)
谢谢您的帮助!