简体   繁体   English

我无法在 Firebase 中创建订单

[英]I can't create an order in Firebase

I'm creating an app with ReactJS.我正在使用 ReactJS 创建一个应用程序。 It's an e-commerce.这是一个电子商务。 After I add some product to the cart I have to complete the form to finish the order.在我将一些产品添加到购物车后,我必须填写表格才能完成订单。 That should send the information to Firebase and create a new collection, but I get the error那应该将信息发送到 Firebase 并创建一个新集合,但我收到错误

Uncaught FirebaseError: Function addDoc() called with invalid data.未捕获的 FirebaseError:使用无效数据调用 Function addDoc()。 Unsupported field value: a function (found in field total in document ventas/GPFfrRZ6mOd7fEZA9Y1a)不支持的字段值:function(在文档 ventas/GPFfrRZ6mOd7fEZA9Y1a 的字段总数中找到)

Here is the code I have in Cart:这是我在购物车中的代码:

import React, { useContext, useState, useEffect } from 'react';
import { CartContext } from '../../Context/CartContext';
import { doc, addDoc, collection, updateDoc, getFirestore } from 'firebase/firestore';
import Swal from 'sweetalert2';
import Form from '../Form';



const Cart = () => {
  const { cart, removeItem, emptyCart, totalCompra } = useContext(CartContext);
  
  const [idVenta, setIdVenta] = useState("");
  
  const finalizarCompra = () => {
    
    const db = getFirestore()
    
    const ventasCollection = collection(db, "ventas");
    addDoc(ventasCollection, {
      items: cart,
      total: totalCompra,
    }).then((result) => {setIdVenta(result.id)});

    cart.forEach((item) => {
      const updateCollection = doc(db, 'items', item.id)
      updateDoc(updateCollection,{
          stock: item.stock - item.quantity,
        })
    });
    
    emptyCart();
  }

  useEffect(() => {
    if (idVenta.length > 0) {
        Swal.fire({
            position: 'top-end',
            icon: 'success',
            title: 'Se ha registrado la compra en el sistema con el siguiente id: '+ idVenta,
            showConfirmButton: false,
            timer: 2000
        });
    }
})

  return (
    <>
      <div>Cart</div>
      {cart.map((product, key) => (
        <div key={key}>
          <div>{product.titulo}</div>
          <p>Precio por unidad: ${product.precio}</p>
          <p>Cantidad: {product.qty}</p>
          <button onClick={ () => {removeItem(product.id)}}>ELIMINAR</button>
        </div>
      ))}
      <button onClick={emptyCart}>VACIAR CARRITO</button>
      <p>PRECIO FINAL: ${totalCompra()}</p>
      <div>
        <Form fCompra={finalizarCompra} />
      </div>
    </>
  );
};
export default Cart;

Either cart or totalCompra is a function. carttotalCompra都是 function。

Since you are getting it from the context and it is not logged anywhere so it is difficult to say which of these or maybe both of these.由于您是从上下文中获取它并且它没有在任何地方记录,因此很难说其中哪一个或这两个。

Try logging both the variables and make sure only proper values are passed.尝试记录这两个变量并确保只传递正确的值。

Also, make sure that undefined is not passed as it is not allowed in addDoc as well.此外,请确保未传递undefined ,因为addDoc中也不允许使用它。

The error message is telling you that one of the fields you're trying to add using to a document using addDoc is actually function, which is invalid.错误消息告诉您,您尝试使用addDoc添加到文档的字段之一实际上是 function,这是无效的。 My guess is that totalCompra is a function.我的猜测是, totalCompra是 function。 What you have to do instead is pass a valid value, such as a string, number, boolean, object, array, or null.您需要做的是传递一个有效值,例如字符串、数字、boolean、object、数组或 null。 If you pass a function, Firestore will not invoke the function for you.如果您传递 function,Firestore 将不会为您调用 function。

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

相关问题 我如何在 firebase 中使用按日期排序 - How can i use order by date in firebase 如何按顺序显示 Firebase Firestore 数据? - How can I display Firebase Firestore data in order? 我无法使用 Swift 将文件上传到 firebase - i can't upload files to firebase with Swift 如何使用 firebase 管理员 SDK 创建具有社交提供者的租户? - How can I create a tenant with social provider with firebase admin SDK? 为什么我的 Firebase Cloud Function 不能使用“allAuthenticatedUsers”? - Why can't I use `allAuthenticatedUsers` for my Firebase Cloud Function? 我想要一个 firebase 文档的下拉列表,但无法获取值 - i want to have a dropdown of firebase document but can`t get the values 我无法访问我的 Firebase Crashlytics 仪表板 - I can't access my Firebase Crashlytics Dashboard 删除后,我可以在 firebase 中通过电话号码重新创建用户身份验证吗? - Can I re create user authentication by phone number in firebase after being deleted? 未找到模块:错误:无法解析“./Firebase” - Module not found: Error: Can't resolve './Firebase' 无法设置firebase实时数据库参考 - Can't set firebase realtime database reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM