在调用云函数时显示Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID 5269cea3-6ea2-11ec-922d-525400868f7d, cloud function service error code -504002, error message Runtime.ImportModuleError: Error: Cannot find module ‘docxtemplates’
Require stack:
-
/var/user/index.js
-
/var/runtime/node12/UserFunction.js
-
/var/runtime/node12/Runtime.engine.js
-
/var/runtime/node12/bootstrap.js
at Object.module.exports.load (:39520/var/runtime/node12/UserFunction.js:34)
at Runtime.handleOnce (:39520/var/runtime/node12/Runtime.engine.js:99)
at Timeout._onTimeout (:39520/var/runtime/node12/Runtime.engine.js:56)
at listOnTimeout (:39520/appservice/internal/timers.js:549)
at processTimers (:39520/appservice/internal/timers.js:492); at cloud.callFunction api;
at new t (WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:17)
at u (WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:17)
at l (WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:17)
at Function.success (WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:17)
at d (WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:2)
at WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:2
at f (WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:26)
at Function.<anonymous> (WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:26)
at d (WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:2)
at WASubContext.js?t=wechat&s=1641432266952&v=2.14.1:2(env: Windows,mp,1.05.2111300; lib: 2.14.1)
云函数的入口文件和本地的JS应该都没有问题,云函数环境也输入了的,就是解决不了?请各位指点哈,感谢!
附代码如下:
云函数:
/ 云函数入口文件
const cloud = require('wx-server-sdk')
const {
createReport
} = require('docxtemplates')
const fs = require('fs');
const path = require('path')
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('../../extend/dayjs');
const preDir = dayjs().format("YY/MM/DD");//日期
const stringRandom = require('string-random')
const randfilename = stringRandom(32)//随机文件名
const cloudPath = `verify/docx/${preDir}/${randfilename}.docx`//文件
return await cloud.uploadFile({
cloudPath,
fileContent: Buffer.from(buffer, 'hex')
})
}
本地的调用页面js文件:
testdocx(){
wx.cloud.callFunction({
// 云函数名称
name: 'testdocx',
// 传给云函数的参数
data: {
},
})
.then(res => {
console.log(res.result)
})
.catch(console.error)
},
本地的app.js文件
App({
onLaunch: function () {
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力');
} else {
wx.cloud.init({
// env 参数说明:
// env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
// 此处请填入环境 ID, 环境 ID 可打开云控制台查看
// 如不填则使用默认环境(第一个创建的环境)
env: 'wxmltodocx-9gnoh53l4e4377ca',
traceUser: true,
});
}
this.globalData = {};
}
});