[英]TypeError: Cannot read properties of undefined (reading 'authService')
[英]TypeError: Cannot read properties of undefined (reading 'buffer')
在前端使用 form-data 發送 post/put 方法在服務器中保存數據和編輯數據。 在前端,圖像字段有問題。 添加新數據時。 新數據將被保存。 但是當他們嘗試編輯數據時,所做的數據更改將不會保存,只有在更改圖像字段時才會保存。 (這個問題只出現在ios)。 在 IOS 中,當他們嘗試在不更改圖像字段的情況下更新數據時,服務器將崩潰
router.put("/:id", authenticate, async (req, res) => {
uploads(req, res, function (err) {
if (err instanceof multer.MulterError) {
return send(res, RESPONSE.FILE_TOO_LARGE);
} else if (err) {
return send(res, RESPONSE.UNKNOWN_ERROR);
}
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: `uploads/${uuid()}`,
Body: req.file.buffer,
};
s3.upload(params, async (error, data) => {
if (error) {
send(res, RESPONSE.UNKNOWN_ERROR);
}
try {
const id = req.params.id;
const updatedData = {
first_name: req.body.first_name,
last_name: req.body.last_name,
phone: req.body.phone,
email: req.body.email,
image: data.Key,
};
const options = { new: true };
await SomeModel.findByIdAndUpdate(id.trim(), updatedData, options);
return send(res, RESPONSE.SUCCESS);
} catch (err) {
return res.status(400).send(err.message);
}
});
聽起來你應該只在存在req.file
的情況下進行 S3 上傳和設置image
router.put("/:id", authenticate, (req, res) => {
uploads(req, res, async (err) => {
if (err instanceof multer.MulterError) {
return send(res, RESPONSE.FILE_TOO_LARGE);
} else if (err) {
return send(res, RESPONSE.UNKNOWN_ERROR);
}
const id = req.params.id;
const updatedData = {
first_name: req.body.first_name,
last_name: req.body.last_name,
phone: req.body.phone,
email: req.body.email,
};
try {
if (req.file) {
const { Key } = await s3
.upload({
Bucket: process.env.AWS_BUCKET_NAME,
Key: `uploads/${uuid()}`,
Body: req.file.buffer,
})
.promise();
updatedData.image = Key;
}
await SomeModel.findByIdAndUpdate(id.trim(), updatedData, { new: true });
return send(res, RESPONSE.SUCCESS);
} catch (err) {
return res.status(400).send(err.message);
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.