Other ways to replace a table row? – JavaScript – SitePoint - STRATEGIES TO EARN MONEY

Breaking

Recientes

Friday

Other ways to replace a table row? – JavaScript – SitePoint

fetch() is not always reliable AFAIK because it is async. I have tried to edit and replace a table row in a reliable way for some days and found this way to replace a table row. Maybe reliable?

  1. Edit record
  2. fetch the record with delay
  3. update the table
async function update_sql(url, json) { let body = get_updatebody(json) let obj = JSON.parse(json); let id = obj[mod_id] await fetch(url, body) .then(response => (response.json())) //no return values .then(get_newrow(id)); finish() tippy2()
} function get_newrow(id) { setTimeout(function() { let url = "https://api3.go4webdev.org/" + mod + "/id/" + id; fetch(url) .then(response => response.json()) .then(newdata => replace_tablerow(newdata)) }, 250);
} function replace_tablerow(data) { alert(JSON.stringify(data)) let row = document.querySelector('table tr[data-id="' + data[mod_id] + '"]'); for (let key in data) { let val = data[key]; let cell = row.querySelector('td[data-key="' + key + '"]'); if (cell) cell.innerHTML = val; }
}

Theoretically I can edit and send return values, but this only seems to result in object.Promise answer. I interpret that this is not reliable at all…

  1. Edit record and send return values
  2. Replace the table row with returned values

Questions:

  1. Is there a even more reliable way to edit a record and update the table row with new values?
  2. Is there a way to read return values in Javascript and update a table row?
  3. Is there any other ways to do this that are even simpler?

I absolutely don’t understand what you want to do?

What means “Edit data”? Who is editing data? Where is it edited?

Why should fetch not being reliable only because its async?

A timeout is never ever a good idea in controlling asynchronous tasks.

He has come up with a partially suitable solution to the problem, and is asking us for advice about how it might be done more suitably without the hack of using the delay.

#1 Not sure how that follows. Asynchronous vs synchronous does not make something more or less reliable; it’s a web connection from the browser to the web host. If the web host isnt reliable, thats not fetch’s fault.

#2 You’re awaiting the first fetch, so its not asynchronous anymore. You’re forcing it to be synchronous. Why you’re not awaiting the newrow fetch, i dont know, but you’ve chosen to make it asynchronous.

mod is undefined, but i will assume you’ve defined it elsewhere, otherwise javascript would be screaming at you in the console. Why you use mod here, but the mod_id from the JSON everywhere else, i’m uncertain. Are you sure this isnt your problem? Are you causing a race condition somewhere to set the value of mod?

Select a row, edit record in database and update selected row.

Edit, update or modify a record in a database.

Fetch() is executed, but you cannot expect the result to be ready to use. It is AFIAK just a “promise” to deliver some data. And sometimes fetch does not always keeps the promise as it takes longer time etc.

You hit the nail. That is why I am asking :slight_smile:

Fetch() does not deliver a “ready” state back AFAIK. So I cannot check if the record is saved before I use it (?). Removing the delay it fetches the old record. The update is not fully saved.

The critical part is to control that the record is properly saved so I can use it to update the table row. I do not know if async await fetching the updated record makes any difference(?)

This is an attempt to make it generic. mod and mod_id is set at the page level (global) and are used to represent usr (table) and user_id (table column primary key) (or tsk and tsk_id for another window).

then is a “completed” state for the request that you sent. If your receiving site is also doing asynchronous tasks to store the record, then there should be a job-completion API for that job, and the remote server should be serving a job reference identifier for you to check the status of the commit; you shouldnt be pulling the record just on a randomly determined timer.

Then why use data[mod_id] in the replace_tablerow? That’s not referencing your global information, it’s referencing what you got back from the server request…

Any example?

let row = document.querySelector('table tr[data-id="' + data[mod_id] + '"]');

means “search and get a reference for this row by using data-id: usr_id=2”. Better suggestions?

I am still confused.

somewhere in your code you send am async fetch to store the changes in the database. This request will return a promise. In the resolve of the promise you can be sure, that the new data is stored. So now you can call the update table and it will, of course, fetch the new data. (Even if this is not necessary because you already have this data send to the server, so why get the same data back?)

Not a direct example, but how it would work normally in the case where a server was taking asynchronous jobs:

Client Sends: “Do this please. Here is the data for ID 9876.”
Server Sends: “Working on it. Job ID 1234.”
(pause)
Client Sends: “Checking Job ID 1234.”
Server Sends: “In Process.”
(pause)
Client Sends: “Checking Job ID 1234.”
Server Sends: “Job complete.”
(At this point, the client knows the job is done on the remote server; what they choose to do at that point is up to them, but in your case, it would appear to be something like)
Client Sends: “Get record 9876”
Server Sends: Record 9876 information.

If that is what your remote server is doing, then that is how i would expect the flow to go; you poll until you see the job is done, and then you can act on it. I dont know the service you’re calling with that URL, so I don’t know if that’s what’s happening. It seems to be what you’re suggesting is happening, because otherwise you wouldnt need a delay at all.

I See no advantage in this double asynchronous tasks. If the client is not waiting for the server to finish, the server must not return before he has finished. Everything else will lead to such a very awful polling structure which I would try to avoid always.

But why does it not work without the delay?

That is a very good question. I have no ready answer to that :slight_smile: Other than it confirms that the record is saved properly.

If you’re running an API for database storage for many clients, you may want to asynchronously store jobs rather than handling each request synchronously and potentially allowing one connection to halt your service for other transmitters.

If I send the API 5 GB of data, or a complex query that’s going to consume processing time, you want to be able to still field other requests while the job chunks away in the background.

Not sure which language/framework is not able to handle multiple requests in parallel without ending the first one, but I would never use it :slight_smile:

My advice would be to never host an API-based data storage solution then. shrug

So I interpret that you suggest a loop that checks for an example a time stamp is edited and saved on the record?

I suggest that if the server ISNT doing your requests synchronously, you’re missing a step in the process. Without the service’s documentation, I can’t give a direct answer.

If it IS doing the request synchronously, then there wouldnt be a need for a delay, you would just send the update, and as soon as it is done, send the select (presumably you’re doing this to verify the remote server has been updated successfully; you’ve already got the data to put into the row, because you’re the one sending it to the remote server).

Maybe you can start by telling us which backend and framework you are using to store the data?

I use Go + Vanilla JS, CSS and HTML (no framework)

The API is 100 percent Go (no framework)

And the database is Postgresql using the built in JSON functions. (A touch of NoSQL)

I have no knowledge of GO so I can’t help much here…

Can you show me how you call the REST api to store the edited data in your JS code?

Techyrack Website stock market day trading and youtube monetization and adsense Approval

Adsense Arbitrage website traffic Get Adsense Approval Google Adsense Earnings Traffic Arbitrage YouTube Monetization YouTube Monetization, Watchtime and Subscribers Ready Monetized Autoblog



from Web Development – My Blog https://ift.tt/lTrSpKM
via IFTTT

No comments:

Post a Comment

Como ganar dinero