Open problem of IndexedDB

  node.js, question

Attach code:
IndexDB.js

var datalist = [
 {id:1,name: "cheng Wen 1", phone: "13237177923", sex: "1", address: "gem garden", home: "fame"},
 {id:2,name: "cheng Wen 2", phone: "13237177923", sex: "1", address: "gem garden", home: "fame"},
 {id:3,name: "cheng Wen 3", phone: "13237177923", sex: "1", address: "gem garden", home: "fame"
 bracket
 
 /**
 * check whether the browser supports indexedDB
 */
 window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
 if(!  window.indexedDB)
 bracket
 Log ("your browser does not support IndexedDB");
 bracket
 
 /**
 :: Create a database
 */
 
 function openDB(name,version){
 var version = version||1;
 var request = window.indexedDB.open(name,version);
 request.onerror = function(){
 Log ("cwdb database creation failed or abnormal ~")
 bracket
 request.onsuccess = function(e){
 cw_DB.db = e.target.result;
 Log ("cwdb database connection succeeded ~")
 console.log(cw_DB)
 bracket
 request.onupgradeneeded = function(e){
 Log ("cwdb database operation ~")
 cw_DB.db=e.target.result;
 var db = e.target.result;
 if(!  db.objectStoreNames.contains("list")){
 db.createObjectStore('list',{keyPath:"id"});
 /*store.createIndex("nameIndex","name",{unique:true})
 store.createIndex("phoneindex","phone",{unique:false});
 store.createIndex("sexindex","sex",{unique:false});
 store.createIndex("addressindex","address",{unique:false});
 store.createIndex("homeindex","home",{unique:false});  */
 bracket
 bracket
 bracket
 
 /**
 * data manipulation
 * @param {Object} db //IndexedDB object
 * @ param {string} stroename//stroename
 * @param {Number} index // subscript
 * @param {Object} value // value
 * @param {String} operation // operation type
 * @param {Object} obj // object properties
 */
 function operaData(parameter){
 var transaction  = parameter.db.transaction(parameter.stroename,"readwrite");
 var stroe = transaction.objectStore(parameter.stroename);
 switch(parameter.operation){
 case "add":
 addData(stroe,parameter.value);
 break;
 case "get":
 getData(stroe,parameter.index)
 break;
 case "update":
 updateData(stroe,parameter.index,parameter.obj,parameter.value);
 break;
 case "del":
 delData(stroe,parameter.index);
 break;
 bracket
 bracket
 
 /**
 * Add data
 * @param {Object} stroe //object store
 * @param {Object} list // added objects
 */
 function addData(stroe,list){
 if(typeof(list)=="object"){
 for (var i =0;  i<list.length;  I plus)
 stroe.add(list[i]);
 bracket
 }else{
 Layer.msg ('added type must be object ~');
 bracket
 bracket
 
 /**
 * Read data
 * @param {Object} stroe //object store
 * @param {Number} index // subscript
 */
 function getData(stroe,index){
 var request = stroe.get(index);
 request.onsuccess = function(e){
 var list = e.target.result;
 return list;
 console.log(list)
 bracket
 bracket
 
 /**
 :: Update data
 * @param {Object} stroe //object store
 * @param {Number} index // subscript
 * @param {Object} obj // object properties
 * @param {Object} value // modified value
 */
 function updateData(stroe,index,obj,value){
 var request = stroe.get(index);
 request.onsuccess = function(e){
 var list = e.target.result;
 list[obj] = value;
 stroe.put(list);
 console.log(list);
 bracket
 bracket
 
 /**
 * delete data
 * @param {Object} stroe //object store
 * @param {Object} index // subscript
 */
 function delData(stroe,index){
 stroe.delete(index);
 bracket

index.js

var run = {db:"",stroename:"",index:"",value:"",operation:"",obj:""};
 var cw_DB ={name:"testDB",version:7,db:null};
 openData();
 var index = angular.module("index",[]);
 index.controller("indexCtrl",function($scope){
 console.log(cw_DB)
 $scope.data={name:"",phone:"",sex:"",address:"",home:""};
 $scope.table_list=opera(cw_DB.db,"list",1,null,"get",null);
 console.log($scope.table_list);
 $scope.add_data= function(){
 $scope.table_list.push($scope.data)
 bracket
 
 
 /**
 * data manipulation
 * @param {Object} db //IndexedDB object
 * @ param {string} stroename//stroename
 * @param {Number} index // subscript
 * @param {Object} value // value
 * @param {String} operation // operation type
 * @param {Object} obj // object properties
 */
 function opera(db,stroename,index,value,operation,obj){
 run.db = db;
 run.index = index;
 run.stroename=stroename;
 run.operation=operation;
 run.obj=obj;
 run.value = value;
 console.log(cw_DB)
 console.log(run)
 operaData(run)
 bracket
 })
 function openData(){
 openDB(cw_DB.name,cw_DB.version);
 bracket

clipboard.png
This is the sequence.

According to the normal process steps should be the following steps output

clipboard.png

clipboard.png

But the result was …

clipboard.png

345 The first thing to do is to finish it, and then to do it. 12. I’m thinking about Why? ? Is it because I separate the IndexedDB into a js file and use another file to retrieve it, which will slow down the opening speed? ?

Just contacted IndexedDB for solution. . .
At that time, when writing IndexedDB’s opening database and adding, deleting, modifying and checking, there seemed to be no problem found in calling in one js. I now put the open method into another JS as a reference. As a result, there was an out-of-order execution that caused the failure to run.

Although you call in order of 1, 2, 3, 4 and 5, onsuccess and onupgradeneeded are executed asynchronously by the browser. So what you see is the execution sequence of 3, 4, 5, 1, 2.
Put operaData into a callback function inside. This callback function is executed while waiting for onsuccess. There should be no problem.
Your problem is to beOnsuccess and onupgradeneeded methods are executed asynchronouslyThe characteristics of the pit. This problem can be solved by using callback functions.