0

I want to know how I can save the id for an object added to an object store?

I know I can get the id with the request.result of the onsuccess function but I want to save like this:

//after a get
var id;
request.onsuccess = function(event){
    id = request.result.id;
}
alert(id); //undefined

I have seen another answers and all say how to show the id in the onsuccess function:

request.onsuccess = function(event){
    alert(request.result.id);
}

but I want to save the id to use outside the onsuccess.

doulos
  • 61
  • 1
  • 4
  • The other answers say how to show the id in the `onsuccess` because *that's the answer*. You simply can not use it in the place where your `alert()` is. Study the information in the duplicate until you understand the coding patterns you should use. –  Nov 18 '15 at 19:06
  • Yes, @squint is right. You cann't use response in a variable. There is an other way to do it. Store response value in a hidden field on form and then on alert call that id. like document.getElementById().value = request.result.id; and then alert(document.getElementById().value); only if response code is 200 – Rohit Batta Nov 18 '15 at 19:13
  • Thanks, i will study the information. And i want the id because has an autoincrement value and it is added aotumatically. – doulos Nov 18 '15 at 19:24
  • More specifically, the generated key will simply not be known until the asynchronous operation has completed. There could be other transactions or requests happening that will generate new keys. – Joshua Bell Nov 18 '15 at 19:37

0 Answers0