简体   繁体   English

异步 function 声明中出现“缺少分号”错误

[英]"Missing semicolon" error on Async function declaration

In a Vue app, I want to group js functions in a js file.在 Vue 应用程序中,我想将 js 函数分组到一个 js 文件中。 In getData.js, I have the following:在 getData.js 中,我有以下内容:

import { collection, getDocs } from 'firebase/firestore/lite';
import { db } from "./firebase/index";
import store from "../store/index"

  
  async getProjects () { 
    const querySnapshot = await getDocs(collection(db, "projects"));
      querySnapshot.forEach((doc) => {
      let project = doc.data()
      project.id = doc.id
      store.state.currentProjectList.push(project)
    });
  }



  export { getProjects } 

I get the "Missing semicolon."我得到“缺少分号”。 error on the line where I declare the function: async getProjects().我声明 function 的行出错:async getProjects()。

(I know this isn't the way to add data to the store but this is only for testing purpose) (我知道这不是将数据添加到商店的方式,但这仅用于测试目的)

Thanks for any help!谢谢你的帮助!

async getProjects () { is method declaration syntax ; async getProjects () {方法声明语法 it is only allowed inside an object or class definition .它只允许在 object 或 class 定义中使用。

To create a local function you need to use a function declaration or an arrow function.要创建本地 function,您需要使用 function 声明或箭头 function。

async function getProjects () { 

or或者

const getProjects = async () => {

(You could also use a function expression, but that gets even further from the syntax you had). (您也可以使用 function 表达式,但这与您所拥有的语法相去甚远)。


You also have a missing semi-colon at the of the previous line – import store from "../store/index" which isn't a JS error (but might be a linting error if you are using a linter and didn't mention that).上一行的分号也不见了import store from "../store/index"这不是 JS 错误(但如果你使用的是 linter 而不是,则可能是一个 linting 错误提到那个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM