云函数使用cloud.uploadFile时出错,怎么解决?
发布于 4 年前 作者 fangchen 14705 次浏览 来自 问答

云函数在return使用cloud.uploadFile,准备把生成的文件上传到云存储,但是始终提示Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID 195ed62a-6ec9-11ec-b78e-5254005a5a71, cloud function service error code -504002, error message TypeErruploadFile:fail or: uploadFile:fail Cannot read property ‘url’ of undefined。

源代码如下:

云函数index.js

// 云函数入口文件
const cloud = require('wx-server-sdk')
const {
    createReport
} = require('docx-templates')
const fs = require('fs');
const path = require('path')
// const dayjs = require('dayjs')

cloud.init({
    env'cloud.DYNAMIC_CURRENT_ENV'
})

// 云函数入口函数
exports.main = async (event, context) => {
    const template = fs.readFileSync(path.join(__dirname, 'verify_company.docx'))//读取文件

    const buffer = await createReport({
        template,
        data: {
            //变量
            no'123',
            creator'Appleseed',
        },
        cmdDelimiter: ['{''}']//以{}作为变量分隔符
    })
            
    const dayjs = require('dayjs');
    const preDir = dayjs().format("YY/MM/DD");//日期
    const stringRandom = require('string-random')
    const randfilename = stringRandom(32)//随机文件名
    const cloudPath = `${preDir}${randfilename}.docx`//文件

    return await cloud.uploadFile({
        cloudPath,
        fileContent: Buffer.from(buffer, 'hex')
    })
    
}

考虑到可能是cloudPath的路径的问题,所以把上面cloudPath的修改成了如下:

const cloudPath = `7778-wxmltodocx-9gnoh53l4e4377ca-1309024106/test/${preDir}${randfilename}.docx`//文件

上面的7778-wxmltodocx-9gnoh53l4e4377ca-1309024106/test/为我的云存储的路径。

但是各种调试都不能解决,系统提示的问题依然一样,请各位老师指点,感谢!

1月7日经老师指点,将cloudPath修改为了固定值,代码如下:

// const dayjs = require('dayjs');
    // const preDir = dayjs().format("YY/MM/DD");//日期
    // const stringRandom = require('string-random')
    // const randfilename = stringRandom(32)//随机文件名
    // // const cloudPath = `7778-wxmltodocx-9gnoh53l4e4377ca-1309024106/test/${preDir}${randfilename}.docx`//文件
    // const cloudPath = preDir+randfilename+'.docx'//文件
//上面这一段代码都注释了不用,直接指定一个导出的文件名。return await cloud.uploadFile({
        cloudPath: 'test1.docx',
        fileContent: Buffer.from(buffer, 'hex')
    })

但是依然报的是同样的错误问题,还请各位老师指点!感谢!!!!

2 回复

1、写死cloudPath试一下。

回到顶部