云函数中嵌套for循环使用await失败,为什么?
发布于 2 年前 作者 zhouyang 11157 次浏览 来自 官方Issues

exports.main = async (event, context) => {

    try {

        if (event.arr1 != null{

            const wb = new xl.Workbook()

            const ws = wb.addWorksheet('列表')

            let StuInfo1 = [];

            StuInfo1 = JSON.parse(event.arr1)

            ws.cell(1, 1).string("上报人员")

            ws.cell(1, 2).string("上报原因")

            ws.cell(1, 3).string("上报时间")

            ws.cell(1, 4).string("上报图片")

            ws.cell(1, 5).string("1")

            ws.cell(1, 6).string("2")

            ws.cell(1, 7).string("3")

            let lg = 0

            lg = StuInfo1.length

            const fileid = 'cloud://cloud1-1graz7ji9c39ec6b.636c-cloud1-1graz7ji9c39ec6b-1316894358/.png'//这里为了测试先在云存储里找了个地址,后期会改为动态的

            for (let key = 0; key < lg; key++{

                ws.cell(key + 2, 1).string(StuInfo1[key].applicant)

                ws.cell(key + 2, 2).string(StuInfo1[key].reason)

                ws.cell(key + 2, 3).string(StuInfo1[key].date)

                ws.cell(key + 2, 4).string(StuInfo1[key].images.length + "")

                ws.row(key + 2).setHeight(65)

                let lg1 = 0

                lg1 = StuInfo1[key].images.length

                let res = await cloud.downloadFile({

                    fileID: fileid

                })

                let image = res.fileContent

                ws.addImage({

                    image: image,

                    type: 'picture',

                    position: {

                        type: 'twoCellAnchor',

                        from: {

                            col: 5,

                            colOff: 0,

                            row: key + 2,

                            rowOff: 0,

                        },

                        to: {

                            col: 6,

                            colOff: 0,

                            row: key + 3,

                            rowOff: 0,

                        },

                    },

                })

                //getImages(StuInfo1[key].images, key, ws)

                // getImages(StuInfo1[key].images, key, ws).then(res => {

                //     ws = res

                // })

            }

            const buffer = await wb.writeToBuffer()

            return await cloud.uploadFile({

                cloudPath: 'export/patrolExport22.xlsx',

                fileContent: buffer, //excel二进制文件

            })

        }

    } catch (error{

        console.error(error)

    }

}

上面是单层for循环使用await,可以正常将图片写入表格,

下面是双层嵌套for循环使用await,图片写入表格失败,只能写入文字。

exports.main = async (event, context) => {

    try {

        if (event.arr1 != null{

            const wb = new xl.Workbook()

            const ws = wb.addWorksheet('列表')

            let StuInfo1 = [];

            StuInfo1 = JSON.parse(event.arr1)

            ws.cell(1, 1).string("上报人员")

            ws.cell(1, 2).string("上报原因")

            ws.cell(1, 3).string("上报时间")

            ws.cell(1, 4).string("上报图片")

            ws.cell(1, 5).string("1")

            ws.cell(1, 6).string("2")

            ws.cell(1, 7).string("3")

            let lg = 0

            lg = StuInfo1.length

            const fileid = 'cloud://cloud1-1graz7ji9c39ec6b.636c-cloud1-1graz7ji9c39ec6b-1316894358/.png'//这里为了测试先在云存储里找了个地址,后期会改为动态的

            for (let key = 0; key < lg; key++{

                ws.cell(key + 2, 1).string(StuInfo1[key].applicant)

                ws.cell(key + 2, 2).string(StuInfo1[key].reason)

                ws.cell(key + 2, 3).string(StuInfo1[key].date)

                ws.cell(key + 2, 4).string(StuInfo1[key].images.length + "")

                ws.row(key + 2).setHeight(65)

                let lg1 = 0

                lg1 = StuInfo1[key].images.length

                //下面写入图片失败,为什么?

                for (let index = 0; index < lg1; index++{

                    let res = await cloud.downloadFile({

                        fileID: fileid

                    })

                    let image = res.fileContent

                    ws.addImage({

                        image: image,

                        type: 'picture',

                        position: {

                            type: 'twoCellAnchor',

                            from: {

                                col:index+5,

                                colOff: 0,

                                row: key + 2,

                                rowOff: 0,

                            },

                            to: {

                                col: index+6,

                                colOff: 0,

                                row: key + 3,

                                rowOff: 0,

                            },

                        },

                    })

                    

                }

                

                //getImages(StuInfo1[key].images, key, ws)

                // getImages(StuInfo1[key].images, key, ws).then(res => {

                //     ws = res

                // })

            }

            const buffer = await wb.writeToBuffer()

            return await cloud.uploadFile({

                cloudPath: 'export/patrolExport22.xlsx',

                fileContent: buffer, //excel二进制文件

            })

        }

    } catch (error{

        console.error(error)

    }

}

请教各位大佬,应该怎么改正?谢谢了…

回到顶部