TypeError [ERR_INVALID_ARG_TYPE]:"path" 参数必须是字符串、Buffer 或 URL 类型之一。收到类型对象 sails.js?
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type object sails.js?
CODE:(我想读取 xlsx 文件并转换成 Json。这是我的 Code.When 我取消注释那些注释行。我也没有收到任何错误和响应。如果我评论那些错误的行。你能帮我吗?)
exports.importSheet = function(req, res, cb) {
// console.log(req.file("files"))
const XLSX = require('xlsx');
const file_name = req.body.file_name
console.log(file_name)
// const fileType = req.file("files")
// req.file("files").read(XLSX, function(err, files) {
// console.log(err)
// if (err) cb(res.serverError(err));
// else{
// console.log(files)
const workbook = XLSX.readFile(req.file("files"));
const sheetNames = workbook.SheetNames;
let sheetIndex = 1;
const df = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[sheetIndex-1]]);
console.log(df);
cb(null, df)
// }
// })
}
解决方案:
var XLSX = require('xlsx');
exports.importSheet = function(req, res, cb) {
console.log(req.file("files"))
const file_name = req.body.file_name
req.file("files").upload(function(err, files) {
if (err) cb(res.serverError(err));
else{
const workbook = XLSX.readFile(files[0].fd);
const sheetNames = workbook.SheetNames;
let sheetIndex = 1;
const df = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[sheetIndex-1]]);
console.log(df);
cb(null, df)
}
})
}
CODE:(我想读取 xlsx 文件并转换成 Json。这是我的 Code.When 我取消注释那些注释行。我也没有收到任何错误和响应。如果我评论那些错误的行。你能帮我吗?)
exports.importSheet = function(req, res, cb) {
// console.log(req.file("files"))
const XLSX = require('xlsx');
const file_name = req.body.file_name
console.log(file_name)
// const fileType = req.file("files")
// req.file("files").read(XLSX, function(err, files) {
// console.log(err)
// if (err) cb(res.serverError(err));
// else{
// console.log(files)
const workbook = XLSX.readFile(req.file("files"));
const sheetNames = workbook.SheetNames;
let sheetIndex = 1;
const df = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[sheetIndex-1]]);
console.log(df);
cb(null, df)
// }
// })
}
解决方案:
var XLSX = require('xlsx');
exports.importSheet = function(req, res, cb) {
console.log(req.file("files"))
const file_name = req.body.file_name
req.file("files").upload(function(err, files) {
if (err) cb(res.serverError(err));
else{
const workbook = XLSX.readFile(files[0].fd);
const sheetNames = workbook.SheetNames;
let sheetIndex = 1;
const df = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[sheetIndex-1]]);
console.log(df);
cb(null, df)
}
})
}