UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'public_id' of undefined
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'public_id' of undefined
嗨,我在这里遇到了问题,该功能运行良好,我所做的验证并没有给我一个错误提示“TypeError: Cannot read 属性 'path' of undefined”,但现在我稍后有问题,在函数的最后 return 部分,如果我只在 1-5 之间上传,我如何解决获取和“类型错误:无法读取未定义的 属性 '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
});
}
}
}
我相信这对你有用,我还看到你忘记在 .findOneAndUpdate
之前添加一个 await
无需对键进行硬编码,您只需遍历对象,然后在最后动态构建响应
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);
}
}
}
嗨,我在这里遇到了问题,该功能运行良好,我所做的验证并没有给我一个错误提示“TypeError: Cannot read 属性 'path' of undefined”,但现在我稍后有问题,在函数的最后 return 部分,如果我只在 1-5 之间上传,我如何解决获取和“类型错误:无法读取未定义的 属性 '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
});
}
}
}
我相信这对你有用,我还看到你忘记在 .findOneAndUpdate
await
无需对键进行硬编码,您只需遍历对象,然后在最后动态构建响应
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);
}
}
}