[英]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. cart
或totalCompra
都是 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.