所以基本上我想下载一个csv文件,然后将其转换为json,但目前我得到的问题是将csv转换为json的函数在量角器中不起作用然后,方法“converter.fromFile”打印空阵列。下面是我的代码
var createUser = require('../pages/createUser.page.js');
var exportPermissions = require('../pages/exportPermissions.page.js');
var cred = require('../../local.credentials.js');
var globals = require('./../../globals.js');
var Converter = require("csvtojson").Converter;
var fs = require('fs')
var until = protractor.ExpectedConditions;
var URL = 'http://' + cred.httpAuth + '@' + cred.server + '/#/page/login';
const csvFilePath='C:/Users/Manoor khan/Downloads/System___exportedPermissions.csv';
var converter = new Converter({});
var permissions;
describe('Use case: MEP-T96 Export permissions', function () {
var createUserSelectors;
var loginPg;
var permissionsMenu;
beforeAll(function () {
createUserSelectors = new createUser();
permissionsMenu = new exportPermissions();
browser.get(URL).then(function () {
console.log('opened URL');
});
});
it('MEP-T96 Export permissions', function (done) {
globals.login('adminUname', 'adminPass').then(function () { console.log('Logged in as admin') });
browser.wait(until.presenceOf(createUserSelectors.adminPage), 20000, 'admin Page taking too long to appear in the DOM');
createUserSelectors.administrationMenu.click().then(function () { console.log('Clicked adminstration menue') });
expect(createUserSelectors.userManagerMenu.isPresent()).toBe(true, 'Couldnt find manager user link');
expect(permissionsMenu.systemPermissionMenu.isPresent()).toBe(true, 'couldnt find create system permission link');
permissionsMenu.systemPermissionMenu.click().then(function () { console.log('Clicked on system permissions menu') });
browser.wait(until.presenceOf(permissionsMenu.permissionSearchBtn), 20000, 'Permission page didnt load in the given time');
expect(permissionsMenu.permissionSearchBtn.isPresent()).toBe(true, 'Permission search button not found');
expect(permissionsMenu.permissionSearchBar.isPresent()).toBe(true, 'Permission search bar not found');
expect(permissionsMenu.exportPermissionsAsCSVBtn.isPresent()).toBe(true, 'Export permission button not found');
permissionsMenu.exportPermissionsAsCSVBtn.click().then(function () { console.log('Clicked on export permission button') });
permissionsMenu.downloadCVSbutton.click().then(function(){
console.log('Clicked on the download CVS button');
}).then(function(){
if (fs.existsSync(csvFilePath)) {
console.log('found');
converter.fromFile(csvFilePath,function(err,result){
if(err){
console.log("An Error Has Occured");
console.log(err);
}
//permissions = result;
console.log(csvFilePath);
console.log(result);
});
} else {
console.log('didnt find');
}
}).then(function(){
}).then(function(){
fs.unlink(csvFilePath, function(){
console.log('--->done');
});
});
答案 0 :(得分:0)
const csvFilePath='<path to csv file>'
const csv=require('csvtojson')
csv()
.fromFile(csvFilePath)
.on('json',(jsonObj)=>{
// combine csv header row and csv line to a json object
// jsonObj.a ==> 1 or 4
})
.on('done',(error)=>{
console.log('end')
})
试试这个^
并且还尝试您的文件路径不包含任何空格。
如果您需要承诺,请尝试
var Promise = require('bluebird');
var Converter = require('csvtojson').Converter;
Promise.promisifyAll(Converter.prototype);
var converter = new Converter();
converter.fromFile(csvFilePath)
.then(function(data){console.log(data)})