微信小程序读取excel数据并批量添加进云数据库 上传excel成功 解析成功 为何云数据库集合中还是没有数据
请个各位大神帮我看看 以下附上图和代码
调用处代码
chooseExcel() {
let that = this
wx.chooseMessageFile({
count: 1,
type: 'file',
success(res) {
let path = res.tempFiles[0].path;
console.log("选择excel成功", path)
that.uploadExcel(path)
}
})
},
uploadExcel(path) {
let that = this
wx.cloud.uploadFile({
cloudPath: new Date().getTime() + '.xls',
filePath: path,
success: res => {
console.log("上传成功", res.fileID)
that.jiexi(res.fileID)
},
fail: res => {
console.log("上传失败", err)
}
})
},
jiexi(fileId) {
wx.cloud.callFunction({
name: "excel",
data: {
fileID: fileId
},
success(res) {
console.log("解析并上传成功", res)
},
fail(res) {
console.log("解析失败", res)
}
})
}
云函数代码
const cloud = require('wx-server-sdk')
cloud.init({
env: "wecon-001-0ge1d4ipff2dc6f0"
})
var xlsx = require('node-xlsx');
const db = cloud.database()
exports.main = async(event, context) => {
let {
fileID
} = event
//1,通过fileID下载云存储里的excel文件
const res = await cloud.downloadFile({
fileID: fileID,
})
const buffer = res.fileContent
const tasks = [] //用来存储所有的添加数据操作
//2,解析excel文件里的数据
var sheets = xlsx.parse(buffer); //获取到所有sheets
sheets.forEach(function(sheet) {
console.log(sheet['name']);
for (var rowId in sheet['data']) {
console.log(rowId);
var row = sheet['data'][rowId]; //第几行数据
if (rowId > 0 && row) { //第一行是表格标题,所有我们要从第2行开始读
//3,把解析到的数据存到excelList数据表里
const promise = db.collection('excel')
.add({
data: {
date: row[0], //日期
bxb: row[1], //白细胞
zxlxb: row[2], //中性粒细胞
hxb: row[3], //红细胞
xhdb: row[4], //血红蛋白
xxb: row[5] //血小板
}
})
tasks.push(promise)
}
}
});
// 等待所有数据添加完成
let result = await Promise.all(tasks).then(res => {
return res
}).catch(function(err) {
return err
})
return result
}
谢谢各位!!!!!!!!!!!
让后端断点看看文件有没有传进去,如果有传进去让后端先保存成本地文件,看看能不能打开
能打开就是后端的导入逻辑的问题,不能打开就是你文件传输有问题