Estoy desarrollando una aplicación de listado y uso firebase como mi back-end, antes de agregar la declaración .orderBy para ordenar los datos creando la fecha, todo funciona bien y puedo actualizar la nueva publicación en la aplicación, pero después de agregar .orderBy( 'createdAt', 'desc') en el listado, no puedo actualizar ninguna publicación nueva y hay un error "TypeError: listingsRef.add no es una función. (En 'listingsRef.add(updatedUploadObjects)', 'listingsRef.add ' no está definido) el código se muestra a continuación:
import { setFavoriteItems } from '../../../favorites/redux' import { firebase } from '../../../api/firebase/config' import ServerConfiguration from '../../../../ServerConfiguration' const savedListingsRef = firebase .firestore() .collection(ServerConfiguration.database.collection.SAVED_LISTINGS) .orderBy('createdAt', 'desc') const listingsRef = firebase .firestore() .collection(ServerConfiguration.database.collection.LISTINGS) .orderBy('createdAt','desc') const ListingCategoriesRef = firebase .firestore() .collection(ServerConfiguration.database.collection.CATEGORIES) .orderBy('order')y esto:
if (selectedItem) { listingsRef .doc(selectedItem.id) .update({ ...updatedUploadObjects, photo: coverPhoto }) .then(docRef => { callback({ success: true }) }) .catch(error => { console.log(error) callback({ success: false }) }) } else { listingsRef .add(updatedUploadObjects) .then(docRef => { if (docRef.id) { listingsRef .doc(docRef.id) .update({ id: docRef.id, photo: coverPhoto }) } callback({ success: true }) }) .catch(error => { console.log(error) callback({ success: false }) }) } }Esto define una CollectionReference :
const savedListingsRef = firebase .firestore() .collection(ServerConfiguration.database.collection.SAVED_LISTINGS) Si revisa ese enlace, verá que una CollectionReference tiene un método de add , lo que significa que puede agregarle un documento.
Esto define una Query :
const savedListingsRef = firebase .firestore() .collection(ServerConfiguration.database.collection.SAVED_LISTINGS) .orderBy('createdAt', 'desc') Si revisa ese último enlace, verá que una consulta no tiene un método de add , por lo que no puede add algo a una consulta.
Querrá mantener la CollectionReference también, para que pueda llamar a add() en eso.