node.js - UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“public_id”
问题描述
嗨,我在这里遇到问题,该函数运行良好,我所做的验证没有给我一个错误,说“TypeError:无法读取未定义的属性'路径'”但现在我稍后遇到问题,在最后一个返回部分该功能,如果我只上传 1-5 张图片,我该如何解决获取和“TypeError:无法读取未定义的属性‘public_id’”?如果我上传 6 张图片,它可以完美运行,如果我上传 2 张,它确实会上传 tu cloudinary 但它给出了我之前在代码的那部分中所说的错误。求求你了,谢谢你
create: async (req, res) => {
var pinturaId = req.params.id;
var imageloop=[];
if(req.files.file0!=undefined){
imageloop.push(req.files.file0);
}
if(req.files.file1!=undefined){
imageloop.push(req.files.file1);
}
if(req.files.file2!=undefined){
imageloop.push(req.files.file2);
}
if(req.files.file3!=undefined){
imageloop.push(req.files.file3);
}
if(req.files.file4!=undefined){
imageloop.push(req.files.file4);
}
if(req.files.file5!=undefined){
imageloop.push(req.files.file5);
}
var array = [];
for (let i = 0; i < imageloop.length; i++) {
await cloudinary.uploader.upload(imageloop[i].path, (err, result) => {
array.push(result)
});
}
if (pinturaId) {
Pintura.findOneAndUpdate({ _id: pinturaId }, { new: true }, (err, paintUpdated) => {
if (err || !paintUpdated) {
return res.status(200).send({
status: "Error",
message: "Error al guardar la imagen"
});
}
if (paintUpdated != null) {
}
return res.status(200).send({
status: "Success",
paints: paintUpdated
});
});
} else {
return res.status(200).send({
status: "Success",
image: array[0].public_id + "." + array[0].format,
imageurl: array[0].secure_url,
image2: array[1].public_id + "." + array[1].format,
image2url: array[1].secure_url,
image3: array[2].public_id + "." + array[2].format,
image3url: array[2].secure_url,
image4: array[3].public_id + "." + array[3].format,
image4url: array[3].secure_url,
image5: array[4].public_id + "." + array[4].format,
image5url: array[4].secure_url,
image6: array[5].public_id + "." + array[5].format,
image6url: array[5].secure_url
});
}
}
}
解决方案
我相信这应该对你有用,我还看到你忘了添加await
一个.findOneAndUpdate
而不是硬编码键,您可以遍历对象,然后在最后动态构建响应
create: async (req, res) => {
var pinturaId = req.params.id;
var imageloop=[];
if (req.files) {
// loops over req.files
Object.keys(req.files).map(key => {
// check if the key name includes the substring "file"
if (req.files[key] !== undefined && key.includes('file')) {
imageloop.push(req.files[key]);
}
});
}
var array = [];
for (let i = 0; i < imageloop.length; i++) {
await cloudinary.uploader.upload(imageloop[i].path, (err, result) => {
array.push(result)
});
}
if (pinturaId) {
// you forgot the "await" here
await Pintura.findOneAndUpdate({ _id: pinturaId }, { new: true }, (err, paintUpdated) => {
if (err || !paintUpdated) {
return res.status(200).send({
status: "Error",
message: "Error al guardar la imagen"
});
}
if (paintUpdated != null) {
}
return res.status(200).send({
status: "Success",
paints: paintUpdated
});
});
} else {
const obj = {
status: 'Success',
}
array.map((_n, index) => {
obj[`image${index}`] = `${array[index].public_id}.${array[index].format}`
obj[`image${index}url`] = array[index].secure_url
})
return res.status(200).send(obj);
}
}
}
推荐阅读
- r - 为什么 tmap/shiny 不删除以前的图层?
- sql - Athena/SQL 查询以获得所需的结果
- r - R:如何从网格创建平均值的热图并用 ggplot 绘制它?
- c# - 如何限制 .NET 中 DynamoDB 中 context.Query 的结果数量
- java - NoSuchMethodError:'void com.google.common.base.Preconditions.checkState
- reactjs - 对象作为 React 子级无效(找到:带有键 {state, addBlog} 的对象)。如果您打算渲染一组孩子,请使用数组
- c++ - std::array 初始化,对于没有默认 ctor 的对象?
- reactjs - 如何使用 React Native 限制换行符的数量?
- c# - NetMQ 多线程的最新示例
- c# - GTK# Widget Gtk:: DrawingArea 不显示或响应信号