简体   繁体   English

Vue js 3 将产品添加到 Firebase 时出错

[英]Vue js 3 Error adding product to Firebase

I'm learning Vuejs while I'm creating a website for our store, and I have a problem connecting with Firebase, Authentication works fine, but CRUD doesn't, it gives me this error:我在为我们的商店创建网站时正在学习 Vuejs,我在连接 Firebase 时遇到问题,身份验证工作正常,但 CRUD 没有,它给了我这个错误:

Uncaught SyntaxError: Unexpected reserved word (at Products.vue...) Uncaught SyntaxError: Unexpected reserved word (at Products.vue...)

Versions:版本:

  • vue: 3.2.36 Vue:3.2.36
  • firebase: 9.8.3火力基地:9.8.3

Code:代码:

import { db } from '../main';
import { collection, addDoc } from "firebase/firestore";

export default {
  name: "Products",
  props: {
    msg: String
  },
  data() {
    return {
      product: {
        name: null,
        price: null
      }
    }
  },
  methods: {
    saveData() {
      try {
        const docRef = await addDoc(collection(db, "products"), this.product);
        console.log("Document written with ID: ", docRef.id);
      } catch (e) {
        console.error("Error adding document: ", e);
      }

    }
  }
};

It must be due to some error in your code.这一定是由于您的代码中的一些错误。 Whenever you get such an error, the standard procedure is to check your code to point out which line is exactly throwing the error.每当您遇到此类错误时,标准程序是检查您的代码以指出哪一行恰好引发了错误。

Also, I notice you are using 'await' without the 'async' function.另外,我注意到您正在使用没有“异步”功能的“等待”。 'await' makes a function asynchronous, so it must be declared inside an async function. 'await' 使函数异步,因此必须在异步函数中声明它。 Use of async await in the function is a shorthand for chaining promises, equivalent to calling a chain of then functions and returning a promise in each of the callbacks in the then function.在函数中使用 async await 是链接 Promise 的简写,相当于调用一个 then 函数链并在 then 函数的每个回调中返回一个 Promise。

Also, you may refer to this documentation which guides how to use async and await in Vue.js.另外,你可以参考这个文档,它指导如何在 Vue.js 中使用 async 和 await。

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

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