score:0
you must use .doc()
when you know the document id and then the query goes as follows:
const docref = firebase.firestore().collection("polja").doc("documentid");
but in case you are looking for documents with a specific field value, then you are looking for .where()
const docref = firebase.firestore().collection("polja").where("id", "==", match.params.id);
this can match multiple documents where the value of that field matches the supplied value. so to control the number or results returned, use a limit clause or paginate your query.
if you use .doc()
function, then the variable doc
in the response is a documentsnapshot. you can then use doc.data()
to get its data.
on the other hand, if you use .where()
, it'll return firebasefirestore.querysnapshot<firebasefirestore.documentdata>
which contains all the documents matching the condition you had specified in the .where(). to access all the documents in that querysnapshot, you must access the docs property like this: snapshot.docs
.
now this will be an array of documents. you can use snapshot.size
to see how many documents have matched your query.
some practical examples to clarify:
//collection in firestore:
users -> {userid as documentid} -> user data in the document
//in this case you know the userid then you can should use the .doc()
const userdocref = firebase.collection("users").doc("userid");
userdocref.get().then((userdata) => {
console.log(userdata.data())
//^ this will contain data in side that document
})
//in this case, you want to find firestore documents based on value of any field present in the document, lets say in this example if user's age is more than 15
const userdocsref = firebase.collection("users").where("age", ">=", 15);
userdocsref.get().then((usersdata) => {
console.log(usersdata.docs)
//^ this will log an array of documents which matched the condition of having the value of field age greater than or equal to 15
})
Source: stackoverflow.com
Related Query
- How to debug this error: Uncaught (in promise) Error: Objects are not valid as a React child
- React - Element type is invalid - how to debug this error?
- I have a question about Cloud Firestore V9 and using React Js. In one of my docs, there is a map. How do I access this map?
- How do I convert this code from Firebase Firestore v8 to Firebase Firestore v9 Modular in React
- How to debug this search filter in react
- React - Cloud FireStore - How can I add pagination to this component?
- How to debug React Router?
- How does one debug NextJS React apps with WebStorm?
- How to add style to React Element if it's created this way?
- How to debug with breakpoints in React Native
- Suddenly React cannot execute the 'create-react-app' command. Why is this happening and how can I solve it?
- React Typescript hook error - This expression is not callable
- How to debug react native apps in visual studio code?
- React/nextJS: How to debug different nodes of SSR react application?
- How to run and debug a react app directly from VSCode?
- How does this React code work on CodePen even if no preprocessor is selected?
- React Native start failed with error code -4094 how to solve this ONCE for ALL?
- How do I fix "Cannot assign to read only property 'style' of object" in this React component?
- How to build debug apk in react native
- How to create a debug apk in react native expo
- How to debug react in vs code ? Error break point set but not yet bound
- how to use curly brackets without it being an expression in react
- How can I remove this switch case when the React Element name is always the same as the switch variable?
- React - How to avoid this kind of inline styling
- How do I solve a NetworkError when attempting to debug a React Native application?
- How could I structure this React Native Section List implementation
- How to methodically debug "Uncaught Invariant Violation" errors in react
- This expression is not callable when destructuring an array of React hooks in TypeScript
- How to Debug a React App in Visual Studio 2019 using the "Blank Node.js" Template
- Reading firestore sub-collection data in react - how to set a parent id in the sub-collection where query
More Query from same tag
- How to call a component onClick of a button in a functional component with MaterialUI Button
- ReactJS convert 2 arrays into table
- Single instance of howler player in React/NextJS
- TypeScript: Expected type comes from property 'children' which is declared on 'IntrinsicAttributes & Prop'
- Why id variable is coming undefined in my react code in todo list Delete component?
- Tensorflow automl model in react
- How to take the input from a react component and pass the length of the input to another react component
- How to validate material-ui-phone-number with Yup
- Push to / Update an Array in React JS State
- How do I access a Map object using React Google Maps API?
- io.on connection being triggered with every emit
- React-dropzone Multi Upload Files Combining with Additional Metadata
- My Grandparent is not re-rendering the Query with new variables and displaying new data, after the state changes through useState()
- Use apollo-link to fetch a query upon any mutation
- Render props component throws Objects are not valid as a React child
- ReactJS conditional dispatch reducer action
- how to aviod empty dynamic li in the react js
- react-toastr 3 with typescript
- How to set cursor position inside Textarea in ReactJs?
- Error compiling NextJS & Sass Global stylesheets when deploying to Vercel
- On select item close dropdown in React?
- Getting Uncaught TypeError: Cannot read property 'profileData' of undefined on Reactjs
- How to add props like fullWidth at particular width like 'sm' in Material UI React
- How can I execute a line after a function is done? in javascript (react native)
- Uncaught (in promise) TypeError: Converting circular structure to JSON only in my put calls
- Compiled with warnings. React
- React Django WebSocket connection challenge
- Adding vertical divider to material-ui AppBar component
- Array.splice works on both state sub-arrays
- Uncaught TypeError: WEBPACK_IMPORTED_MODULE_1__.default is undefined (React, SASS,SCSS)