我正在使用multer在expressjs上传图像,并且API在POSTMAN中工作正常,但每当我上传angular5中的图像以保存在mongodb时,我都会收到错误500服务器内部错误。是否有人可以建议我如何在数据库中上传图像?
// API to upload the image and working fine in POSTMAN
router.put('/users/:email', upload.single('image') , (req, res)=> {
console.log(req.file);
User.updateOne({email: req.params.email}, {
$set: {
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: req.body.password,
dob: req.body.dob,
designation: req.body.designation,
address: req.body.address,
mobile: req.body.mobile,
image: req.file.path
}
},
function(err, item) {
if(err) {
res.json(err);
} else {
res.json({message: "record updated successfully!!"});
}
}
)
});
以下是.ts文件,其中包含以下详细信息:
onFileSelected(event) {
console.log(event);
this.selectedFile = event.target.files[0];
}
// to edit the details
updateProfile(editForm) {
const fd = new FormData();
fd.append('image', this.selectedFile, this.selectedFile.name);
const newItem = {
email: editForm.value.email,
firstName: editForm.value.firstName,
lastName: editForm.value.lastName,
dob: editForm.value.dob,
password: editForm.value.password,
designation: editForm.value.designation,
address: editForm.value.address,
mobile: editForm.value.mobile,
image: editForm.fd
};
this.dataService.updateUser(newItem)
.subscribe(item => {
console.log(item);
this.fetchUserData();
this.showEditForm = !this.showEditForm;
},
err => {
console.log(err);
}
);
}