// 云函数入口文件
const cloud = require('wx-server-sdk')
const xlsx = require('node-xlsx')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const db = cloud.database()
const _ = db.command
async function exportFile(params = {}) {
console.log(params, 'params');
const {total} = await db.collection('tk_artist').count()
console.log(total);
const data = []
let dataCVS = '订单导出_' + new Date().getTime() + ".xlsx"
let size = 100
for (let i = 0; i*size < total; i++) {
const skip = i * size
console.log(skip, 'skip');
const artistResult = await db.collection('tk_artist').aggregate().lookup({
from: 'tk_order',
localField: '_id',
foreignField: 'artistId',
as: 'orderList'
}).skip(skip).limit(size).end()
const list = artistResult.list
for (let i = 0; i < list.length; i++) {
let artist = list[i]
let orderList = list[i].orderList
for (let j = 0; j < orderList.length; j++) {
let order = orderList[j]
data.push([
artist.name,
artist.phone,
artist.address,
artist.source,
order.name,
order.phone,
order.cityName,
order.address,
order.coupletLeft,
order.coupletRight,
order.coupletCenter,
])
}
}
}
var buffer = await xlsx.build([{
name: "订单",
data: data
}]);
//4,把excel文件保存到云存储里
return await cloud.uploadFile({
cloudPath: dataCVS,
fileContent: buffer, //excel二进制文件
})
}
// 云函数入口函数
exports.main = async (event, context) => {
switch(event.type) {
case "exportFile":
return await exportFile(event.data)
default:
return
使用了node-xlsx后上传云函数一直失败,用本地调试没有报错
已解决,上传全部文件即可