问题描述
我正在尝试在节点js中创建一个后更新系统,文本保存在数据库中,图像保存在Amazon AWS上。
我遵循的步骤如下: 1)我使用javascript从前端获取信息。
main.j s
import {updatePostFNC} from './updatePosts';
//Update Post
const updatePosts = document.querySelector('.update_creative_point_update_posts');
if(updatePosts){
updatePosts.addEventListener('submit',(upPos)=>{
upPos.preventDefault();
const postIdTOUpdate = document.getElementById('creative---Point19981022-post_id').value;
const postEditSlug = document.getElementById('creative---Point19981022-post_slug').value;
let filesImg = document.getElementById('creative---Point19981022-photo-update').files;
const updatePosts = new FormData();
updatePosts.append('title',document.getElementById('creative---Point19981022-post_title').value);
updatePosts.append('link',document.getElementById('creative---Point19981022-post_link').value);
updatePosts.append('description',document.getElementById('creative---Point19981022-post_description').value);
for (let p = 0; p < filesImg.length; p++) {
updatePosts.append('images',filesImg[p]);
}
updatePostFNC(postIdTOUpdate,postEditSlug,updatePosts);
});
}
updatePosts.js
import "regenerator-runtime/runtime";
import axios from 'axios';
import { showAlert } from './alert';
export const updatePostFNC = async(idPost,slugPost,dataPost)=>{
try{
const updatePost = await axios({
method:'PATCH',url:`/api/v1/post/update-post/${idPost}`,data:dataPost
});
if(updatePost.data.status === 'success'){
showAlert('success','Post was updated successfully!!!');
}
}catch(err){
console.log(err)
}
}
updatePostController.js
const multer = require('multer');
const uniqid = require('uniqid');
const multerS3 = require('multer-sharp-s3');
const aws = require('aws-sdk');
const dotenv = require('dotenv');
const Post = require('../module/postModule');
const catchAsync = require('../utiles/catchAsync.js');
const AppError = require('../utiles/appError');
//Dotenv Settings
dotenv.config({path:'./config.env'});
//1)Config connection to Amazon AWS
aws.config.update({
secretAccessKey: process.env.SECRETACCESSKEY,accessKeyId: process.env.ACCESSKEYID,region: process.env.REGION
});
//2)Connection and Upload Files
const s3 = new aws.S3();
//3)Check if uploaded fille is an image
const multerFilter = (req,file,cb)=>{
if(file.mimetype.startsWith('image')){
cb(null,true)
} else{
cb(new AppError('Not an image! Please upload only images',400),false);
}
};
const uploadPostMultiplePhoto = multer({
storage: multerS3({
s3: s3,Bucket:'carsworld-posts-photo',ACL:'public-read',resize: {
width: 1440,height: 767
},key: function (req,cb) {
cb( null,path.basename( file.originalname,path.extname( file.originalname ) ) + uniqid() +
path.extname( file.originalname ))
}
}),fileFilter:multerFilter
}).array( 'images',10 );
//Update Post
exports.updatePost = catchAsync(async(req,res,next)=>{
uploadPostMultiplePhoto( req,( error ) => {
if( error ){
console.log( 'errors',error );
res.json( { error: error } );
} else {
// If File not found
if( req.files === undefined ){
console.log( 'Error: No File Selected!' );
res.json( 'Error: No File Selected' );
}else{
let fileArray = req.files,fileLocation;
req.body.images = []
for ( let i = 0; i < fileArray.length; i++ ) {
fileLocation = fileArray[i].Location;
req.body.images.push(fileLocation);
}
//Save the file name into database
const update = Post.findByIdAndUpdate(req.params.postId,req.body,{
new:true
});
res.status(200).json({
status:"success",data:update
});
}
}
});
});
但最终会发生错误
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)