Updating JSON data using fetch API

Hi everyone, I'm currently working on an email application and need help with implementing an archive button for each email. It seems that the function to change the archived status to true is being called, but for some reason, it's not making the change.

Interestingly, when I replace "archived: true" with "read: true", the 'read' status does get changed to true. However, it doesn't work for the archived attribute.

I've double-checked and confirmed that 'archived' is a valid attribute in the JSON API.

Any ideas why this might be happening? Here's the relevant code snippet:

function archive_email(id) {
    
    fetch('/emails/' + id, {
    method: 'PUT',
    body: JSON.stringify({
    archived: true
      })
    })
    load_mailbox("inbox");
     
}

Answer №1

It seems like this could be an issue related to promises. Have you had a chance to test out the code snippet below?

const toggle_archive = (email_id) => {
    fetch('/emails/' + email_id, {
        method: 'PUT',
        body: JSON.stringify({
            archived: true
        })
    }).then(() => {
        load_mailbox("inbox");
    })
}

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

Learn how to incorporate additional rows into a table by pressing the plus button within the table with the help of Angular

I require some assistance. I am looking to dynamically generate a row after clicking on the plus button using angular.js. The newly created row should contain an ID and model generated dynamically. Here is an overview of my code: <table class="table ta ...

Merge two JSON arrays without specifying which fields to combine explicitly

After converting two Excel sheets to JSON using an online tool, two separate JSON properties were created for each sheet. The resulting JSON example looks like this: { "Product Info": [ { // Product information details here }, ...

Unhook, add at the beginning, and set requirements jQuery

There are two clickable div elements that can switch classes when clicked. If the first one contains "1", it will be given a class of "active". If the second one contains "2", it will have the class "active" while removing the class from the first one. &l ...

Grab the last item from jq output (not an array)

Looking to extract the last item labeled as "date" from the data provided? The desired result should be: "2019_10_29_12_01_01" $ cat snapshots.json | jq '.snapshots[] | select (.state == "SUCCESS") | {date: .snapshot}' { "date": "2019_10_21_1 ...

Guide to sending post variables in an AngularJS service

Is there a way to send POST variables to a RESTful API using an angularjs service? I currently have the following setup: angularjsServices.factory('LoginService', [ '$resource', function($resource){ return function(user, pass){ ...

The data display in MUI-Datatable is experiencing an issue where it is unable to read properties of undefined, specifically when trying to split the data

Whenever I include data within the MuiDatatable, it triggers this error: Cannot read properties of undefined (reading 'split') Is there a solution to this problem so that the data can be properly displayed? To demonstrate the issue, I have se ...

Adding navigation buttons to a Material-UI Select component: A step-by-step guide

Currently, I have integrated a Select component from MUI and it is functioning well. When an item is selected from the list, the router automatically navigates to that location: This is the snippet of code being used: export default function SelectLocatio ...

Having trouble retrieving information from a nested JSON array within a JSON object using JavaScript

I am facing a challenge with extracting specific information from a JSON object that contains an array nested inside. Even though I have experience working with JSON arrays, this particular object is proving to be tricky. For example, when attempting to r ...

I require displaying the initial three letters of the term "basketball" and then adding dots

Just starting out with CSS and struggling with the flex property. Seems to work fine at larger sizes, but when I reduce the screen size to 320px, I run into issues... Can anyone help me display only the first three letters of "basketball ...

Leverage JavaScript to retrieve the number of active Ajax requests in JSF

When running Selenium tests using JS, I want to ensure that there are no active Ajax requests. While I can successfully extract the amount of active Ajax requests for jQuery and PrimeFaces, I am facing some issues with JSF. String jsPF = "return PrimeFace ...

Using NodeJS/Express to deliver a JSON response in ISO-8859-1 format

I've encountered a challenge with the encoding of JSON data from an API I built using Node and Express. The web application that needs to read this data only accepts ISO-8859-1 encoded JSON, causing some difficulty. Despite following the methods outl ...

Transitioning create-react-app from JavaScript to Typescript

A few months ago, I began a React project using create-react-app and now I am interested in transitioning the project from JavaScript to TypeScript. I discovered that there is an option to create a React app with TypeScript by using the flag: --scripts-v ...

Struggling to create a C# class structure for a JSON string

Currently, I am faced with the challenge of deserializing an object into a custom class. The string that will be passed to me has the following format: {nodename:"node1", version:"v1", PARM1:"p1", PARM2:"p2" ,…, PARAMN:"pn"}. From what I understand, I ...

Jasmine is having trouble scrolling the window using executeScript

I usually use the following command: browser.driver.executeScript('window.scrollTo(0,1600);'); However, this command is no longer working. No errors are showing in the console, making it difficult to troubleshoot. Interestingly, the same scri ...

What is the best way to access the final value from a JSON-extracted variable?

While testing the API, I made a request and received a JSON response. Using the JSON extractor, I was able to extract the ID from the JSON data and store it in a variable. The HTTP request configuration is shown below: https://i.sstatic.net/eF26r9vI.png h ...

Is it possible for JavaScript to interact with elements that are created dynamically?

It's puzzling why the newly generated elements remain unseen by the next function that is called. Any insights on how to address this issue would be greatly appreciated! Resolve: Incorporate async: false to deactivate the asynchronous feature in order ...

There seems to be an issue with accessing the Angular module, even though it has been clearly

While attempting to execute the code below, two errors are encountered: Error: [$injector:nomod] Module 'rooms' is not available! The module name is spelled correctly and loaded dependencies have been included. The modules are structured in the c ...

Issue with deploying Azure node.js application due to libxmljs error

My goal is to launch my website using libsmljs built on node.js on Windows Azure. However, during the deployment process on Azure, I encounter a failure with the following error message: Command: C:\DWASFiles\Sites\CircuitsExpress\Virt ...

Is there a way to incorporate my JavaScript code into my page.jsx file efficiently?

Can I integrate this javascript code from a .js file into my .jsx file seamlessly? const { exportTraceState } = require("next/dist/trace"); const toggleBtn = document.querySelector('.toggle_btn') const toggleBtnIcon = document.querySe ...

Using socket.io to listen for events and wait for promises to resolve

I am facing an issue with a button that communicates with the server to verify if a value entered in an input box already exists. The current code is as follows: $("#button").click(function () { var exists = false; var name = $("#name").val(); ...