当前位置: 首页 > 知识库问答 >
问题:

ASYNC/AWAIT SyntaxError:AWAIT仅在异步函数和顶级模块体中有效

经炜
2023-03-14

我正在做一些非常简单的测试,关于使用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)=>{
    console.log(jsonObj);
    /**
     * [
     *  {a:"1", b:"2", c:"3"},
     *  {a:"4", b:"5". c:"6"}
     * ]
     */ 
})
 
// Async / await usage
const jsonArray=await csv().fromFile(csvFilePath);

我主要关注的是

//异步/等待使用

const jsonArray=wait csv()。fromFile(csvFilePath);

代码的部分。这是我的密码

// const JSONtoCSV = require("json2csv")
// 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 is only valid in async functions and the top level bodies of modules
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47

const data = await CSVtoJSON().fromFile('./input.csv');
             ^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at 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)

我不明白这为什么不起作用。

编辑:我做了@tasobu指出的更改。现在我得到的只是一个回报

const data = (async () => {
    return await CSVtoJSON().fromFile('./input.csv')
})

console.log(data)
Debugger attached.
Promise { <pending> }
Waiting for the debugger to disconnect...

共有3个答案

颛孙兴旺
2023-03-14

你可以在这里使用生活(在这里阅读更多)

(async function main () {
    // You can use await inside this function block
})();
黎鹤轩
2023-03-14

将整个代码放入一个异步函数中并调用它,这样除了函数外的一些const语句和函数/类声明之外,就不会有其他东西了:

async function main () {
  // All code here, can use await
}

main().then(() => process.exit(0), e => { console.error(e); process.exit(1) })
隗轶
2023-03-14

一个Promise在将来返回一些东西,所以你需要一个等待它的方法。

// const CSVtoJSON = require("json2csv")
// const FileSystem = require("fs")
let temp = undefined;

async function test()
{
    const data = await CSVtoJSON().fromFile('./input.csv')
    // do your stuff with data
    
    temp = data;

    console.log(temp) // at this point you have something in.
    return true
}

test();
console.log(temp) // at this point nothing was there.
 类似资料:
  • 错误出现在第1行,我从Ganache获取帐户,但仅对有效 我应该对此代码进行哪些更改以删除错误?请帮帮我。 如果我删除这一行,错误表明它无法访问帐户,并且在此之后,不起作用。 有没有办法把这段代码做成ASYNC函数的形式?

  • 试图使一个chrome扩展刮一个网站,但它总是在内容完全加载之前(在manifest.json的文档读取后尝试) 每次有人叫我: 还尝试:

  • 在开始之前,我承认有几个问题可能听起来与我的标题类似,但是,我读到的所有问题都比我的代码更复杂,解释似乎与我的情况无关。 有人能帮我理解我的代码(下面的代码片段)中发生了什么导致了这个错误吗 未捕获SyntaxError:await仅在异步函数和顶级模块体中有效。 据我所见,导致错误的位于“顶层”主体中。或者是顶级身体的其他意思?谢谢! 编辑区别于其他建议(类似)的问题在这里:我的问题不涉及htt

  • 异步编程对JavaScript语言太重要。Javascript语言的执行环境是“单线程”的,如果没有异步编程,根本没法用,非卡死不可。 ES6诞生以前,异步编程的方法,大概有下面四种。 回调函数 事件监听 发布/订阅 Promise 对象 ES6将JavaScript异步编程带入了一个全新的阶段,ES7的Async函数更是提出了异步编程的终极解决方案。 基本概念 异步 所谓"异步",简单说就是一个

  • 我在中写了这段代码 然后我尝试在另一个文件中使用它 我得到一个错误 “等待仅在异步函数中有效” 问题是什么?

  • 我正在尝试设置一些数据在使用这个节点js代码的FIRESTAR: 在控制台中运行文件: 给我这个错误: 如何解决这个问题