Home > Software design >  Promise on Reactnative
Promise on Reactnative

Time:03-01

I write a promise to insert data in sqlite:

var promise_insertData=new Promise(function(resolve, reject){
          db.transaction((tx) =>{
                tx.executeSql('INSERT INTO chontact (recordID,user, phone) VALUES (?,?,?)',[recordID, name, number],
                (tx, results) => {
                    var status=(results.rowsAffected > 0) ? true : false;
                    if(status)
                        resolve()
                    else
                        reject()
                    }
                );
              });
                        reject()
})

when I try to insert data I got error

promise_insertData(db,recordid,name,phone).then(function(result){
                Toast.show('ok...');
                del_item();
            }).catch(function(error){
                Toast.show('error');
              })

Error:

TypeError: (0, _$$_REQUIRE(_dependencyMap[12], "./JSfiles/functions").promise_insertData) is not a function. (In '(0, _$$_REQUIRE(_dependencyMap[12], "./JSfiles/functions").promise_insertData)(db, recordid, textinput, textinputphone)', '(0, _$$_REQUIRE(_dependencyMap[12], "./JSfiles/functions").promise_insertData)' is an instance of Promise)

CodePudding user response:

Give this a try. You should be able to promise_insertData(stuff).then(doSomethingElse) I'm not sure about the final reject() in your code, that might be wrong.

const promise_insertData = (db, recordid, name, phone) => {
  return new Promise(function(resolve, reject){
    db.transaction((tx) =>{
      tx.executeSql('INSERT INTO chontact (recordID,user,phone) VALUES (?,?,?)',[recordid, name, phone],
      (tx, results) => {
          var status=(results.rowsAffected > 0) ? true : false;
          if(status)
              resolve()
          else
              reject()
      });
    });
    reject() // ??
  })
};

CodePudding user response:

I Wrap my Promise inside a function

  • Related