我正在做一些非常簡單的測驗,關于使用csvtojson節點模塊讀取csv檔案到json格式,我使用下面的代碼作為模板
。a,b,c
1,2,3。
4,5,6。
*/
const csvFilePath='<path to csv file> '
const csv=require('csvtojson')。
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>/span>{
console.log(jsonObj)。
/**。
* [
* {a: "1", b: "2", c: "3"},
* {a: "4", b: "5". c: "6"}.
* ]
*/
})
// Async / await用法
const jsonArray=await csv().fromFile(csvFilePath)。
我主要關注的是
// Async / await用法
const jsonArray=await csv().fromFile(csvFilePath);
代碼的部分。對了,這就是我的代碼
// const JSONtoCSV = require("json2csv")/span>
// const FileSystem = require("fs")。
async function test()
{
const data = await CSVtoJSON() 。 fromFile(' ./input.csv')
return data
}
let temp = await test()
console.log(temp)。
無論我以何種方式嘗試,都會出現以下錯誤
let temp = await test()
^^^^^
SyntaxError。await只在async函式和模塊的頂層機構有效。
在Object。 compileFunction (node: vm: 352: 18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
在Module。 _compile (node:internal/modules/cjs/loader:1065: 27)
在Object.Module._extensions。 js (node:international/modules/cjs/loader:1153:10)
在Module。 load (node:internal/modules/cjs/loader: 981: 32)
在Function.Module。 _load (node:internal/modules/cjs/loader:822:12)
在Function。 executeUserEntryPoint [as runMain] (node: internal/modules/run_main:79: 12)
在node:internal/main/run_main_module:17:47。
或者
const data = await CSVtoJSON() 。 fromFile('./input.csv') 。
^^^^^
SyntaxError。await只在async函式和模塊的最高層機構中有效。
在Object。 compileFunction (node: vm: 352: 18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
在Module。 _compile (node:internal/modules/cjs/loader:1065: 27)
在Object.Module._extensions。 js (node:international/modules/cjs/loader:1153:10)
在Module。 load (node:internal/modules/cjs/loader: 981: 32)
在Function.Module。 _load (node:internal/modules/cjs/loader:822:12)
在Function。 executeUserEntryPoint [as runMain] (node: internal/modules/run_main:79: 12)
在node:internal/main/run_main_module:17:47。
如果我把代碼換成像這樣的全部頂層代碼
const CSVtoJSON = require("csvtojson")
// const JSONtoCSV = require("json2csv")。
// const FileSystem = require("fs")。
const data = await CSVtoJSON() 。 fromFile(' ./input.csv')
console.log(data)
我不明白為什么這沒有用。
EDIT:我做了些什么?
編輯:我按照 @tasobu 指出的方法進行了修改。現在我所得到的是一個回傳的承諾
const data = (async ( ) => {
return await CSVtoJSON().fromFile(' ./input.csv' /span>)
})
console.log(data)
Debugger附加。
Promise { <pending> }
等待 for the debugger to disconnect...
uj5u.com熱心網友回復:
把你的全部代碼放到一個異步函式中并呼叫它,所以除了一些const陳述句和函式/類的宣告外,你不會有任何其他東西:
async function main(){
//所有代碼在這里,可以使用 await。
}
main().then(() =>程序。 exit(0), e => { console. error(e); process.exit(1) })
uj5u.com熱心網友回復:
你可以在這里使用一個IIFE(閱讀更多關于它的資訊這里)
(async function main ( ) {
//你可以在這個函式塊內使用await。
})();
uj5u.com熱心網友回復:
一個Promise回傳的東西在未來所以你需要一種方法來等待它。
// const CSVtoJSON = require("json2csv")/span>
// const FileSystem = require("fs").
let temp = undefined;
async function test()。
{
const data = await CSVtoJSON() 。 fromFile(' ./input.csv')
//用資料做你的事情。
temp = data;
console.log(temp) //在這一點上,你有東西在。
return true
}
test()。
console.log(temp) //此時什么都沒有。
uj5u.com熱心網友回復:
如果你不想使用 await 部分,你必須直接省略它。
const csvFilePath = "./data.csv"
const csv = require("csvtojson"/span>)
csv()
.fromFile(csvFilePath)
.then((jsonObj) => {
console.log(jsonObj)
})
// Async / await usage。
//const jsonArray = await csv().fromFile(csvFilePath)。
或者:
// Async / await用法
async function csvToJson() {
const res = await csv().fromFile(csvFilePath)
console.log(res)
}
csvToJson()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/318060.html
標籤:
