【问题标题】:UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'public_id' of undefinedUnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“public_id”
【发布时间】:2021-02-08 12:59:22
【问题描述】:

嗨,我在这里遇到了问题,该功能运行良好,我所做的验证没有给我一个错误,说“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
            });
        }
    }

}

【问题讨论】:

    标签: node.js validation undefined


    【解决方案1】:

    我相信这应该适合你,我还看到你忘记在.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);
            }
        }
    }
    

    【讨论】:

    • 你真是个天才,非常感谢,它有效,只为看到这个的人,首先你忘记关闭这个“if (req.files[key] !== undefined && key .includes('file')" 你错过了一个 ")" 他们因为第一个图像从 0 开始我当然必须将我前面(角度)和后端的数据库模型从 image 更改为 image0 直到 5 ,当我使用 *ngFor 时也是如此,显然,就是这样,令人难以置信,再次,非常感谢你
    • 作为旁注,我建议采用如下严格的 API 响应模式,我的公司几年前在一位顾问的建议下开始使用它,它一直是我们的主要内容 - narwhl.com
    猜你喜欢
    • 2021-05-20
    • 2021-04-18
    • 2020-11-11
    • 1970-01-01
    • 2021-09-28
    • 2021-05-18
    • 2021-01-15
    • 2021-03-03
    • 2021-04-09
    相关资源
    最近更新 更多