Exploring the capabilities of batch updates in Firestore with the latest web version-9

I am facing issues when trying to update certain values in my firestore collection within my nuxt project.

  const batch = writeBatch(firestore);

  const data = query(collection(firestore, `notifications`, `${uid}/news`));

  const querySnapshot = await getDocs(data);

  querySnapshot.forEach(doc => {
    if (doc.data().type === 'like') {
      batch.update(doc, { seen: true });
    }

     batch.commit();
  });

Answer №1

Ensure to provide a DocumentReference as the first parameter of the update() function, not a QueryDocumentSnapshot.

Remember to execute the batch commit outside of the loop because committing the batch should only occur once. The error message you mentioned in your question is likely related to this.

Additionally, you can skip using the query() method if you intend to iterate through the entire collection.

 const batch = writeBatch(firestore);

 const data = collection(firestore, `notifications`, `${uid}/news`);

 const querySnapshot = await getDocs(data);

 querySnapshot.forEach(doc => {
   if (doc.data().type === 'like') {
     batch.update(doc.ref, { seen: true });
   }
  });

  batch.commit();
 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to dynamically update a specific value within an object based on the current state in React/Next?

I have implemented a Context API where an object is set, and when the user clicks, the state changes. I need to update a specific value with the new state value. const initialState = { notification: false, setting: false, profile: false, } exp ...

Include the button beneath the Rating section using JQuery or AJAX

I am having trouble adding buttons after the result.date in my code. Placing the buttons between td tags is causing an [object Object] error to show up. $.ajax({ type: 'GET', url: 'someUrl.php', data: {op : "demo"}, da ...

Using .attr() to change the 'id' in jQuery will not be effective

Initially, the code has been streamlined to include only the necessary components. Here is the HTML file (single_lesson.html) in question: <tr class="my_lessons"> <td> <select name="my_von" id="my_von"></select> &l ...

Issue with Material UI Autocomplete: onChange event not firing

When using Material UI's Autocomplete example, I am trying to choose an option using keyboard events: Navigate through options with the Up and Down arrow keys Press ENTER to select the desired option However, the onChange event is not being triggere ...

Unable to redirect with Asp response.redirect

I have a Login popup form where I use an ajax post request to Login.asp script in order to prevent the page from going to the POST URL after submission. <script> $(function() { $('#contactForm').submit(function(e){ e.preventDe ...

Customize cell design in Vuetify according to the type of item

Is it possible to format dynamically created columns based on a specific property? In cases where the item does not have that property, it should default to the standard behavior. Below is an example of what I'm attempting to achieve (though the synt ...

If a 404 error is encountered, Vue will automatically redirect the user back to the home page

In my Vue application, I have set up the following routes: export default new Router({ mode: 'history', routes: [ { path: '/', name: 'input', component: Input }, ...

Adding a fresh HTML element to a list at a designated location

I found this excellent example on CODEPEN Is there a way to insert a new random number into the list so that it appears after 24 but before 19? Is there an efficient solution or do I need to manually check each li element and determine where to place the ...

Error: Invalid character encountered during login script JSON parsing

I found this script online and have been experimenting with it. However, I encountered the following error: SyntaxError: JSON.parse: unexpected character [Break On This Error] var res = JSON.parse(result); The problem lies in the file below as I am unf ...

React function causing website to freeze upon dispatch

I created a function in the child component to handle checkbox selection and trigger setDispatch(true). Unfortunately, whenever I check the checkbox, the website freezes and stops responding until I close and reopen it. Here is the function: const [ ...

Reset the select boxes when a button is clicked

I'm currently utilizing Devextreme within my Angular application, and I have three dx-selectbox elements in the component. I am attempting to clear all three dropdown selections when clicking a "clear" button. Unfortunately, I am unable to find a way ...

Transfer password securely through an ajax call

Is it secure to send a password through an Ajax request? I have a login box that makes an Ajax request to check the login credentials and receive a JSON Object with any errors. Would it be better to use form redirection instead? [EDIT] Storing the encry ...

New example of how to use the "useSession" syntax in Next Auth

Currently, I am delving into the next-auth documentation and feeling perplexed by the syntax used in the useSession hook. The way it's showcased in the documentation is as follows: const { data: session, status } = useSession() My confusion stems f ...

Creating a nested array of objects using a recursive function

How can I utilize my getChildren() function to create a larger function that takes my two main arrays objs and objRefs, and generates a single array of objs showcasing their parent/child relationship? Below are the two primary data arrays: const objs = [ ...

What is the method for HTML inline handlers to retrieve the global window object and the variables contained within it?

During my coding test, I encountered an interesting scenario. I had a function called write and used a button with an inline onclick handler to trigger the write() function. function write(text) { alert(text) } <button onclick='write("Some tex ...

using Vue.js to create a multi-page application

As I delve into using vuejs within a multi-page app, I find myself faced with a challenge. Let's consider two pages: /home and /account. To handle these, I've created separate js files - home.js for home.html and account.js for account.html. Whil ...

Modify hyperlink address according to chosen option

Looking to incorporate a select input in Laravel using the latest alpine.js build. Here's what I have in mind: {{ Form::select('dogs', $dogs) }} This utilizes LaravelCollective HTML for streamlined form creation. Once an option is chosen, ...

Sending HTML parameters to a PHP file

I have been trying to pass the parameters from the current HTML page to a PHP page using a form. In my header in the HTML, I currently have the following function: <script> function getURLParameter(name) { return decodeURIComponent((new Re ...

What is the process to utilize Vue2 webcomponent slots within Vue3?

I am facing an issue with integrating a Vue2 web component, installed via NPM, into my new Vue3 project. The problem arises when trying to utilize the component's slots. Even though there are no errors in the following code snippet, the content insid ...

Vuejs allows objects to trigger the execution of methods through elements

My goal is to utilize a function in order to individually set the content of table cells. In this specific scenario, I aim to enclose the status with the <strong> - Tag (I refrain from modifying the template directly because it is stored within a com ...