UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“public_id”

时间:2021-02-07 01:27:45

标签: node.js validation undefined

嗨,我在这里遇到了问题,该功能运行良好,我所做的验证并没有给我一个错误,说“类型错误:无法读取未定义的属性‘路径’”但现在我稍后遇到了问题,在最后返回函数的一部分,如果我只上传 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
            });
        }
    }

}

1 个答案:

答案 0 :(得分:1)

我相信这对您有用,我还看到您忘记在 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);
        }
    }
}