-1

I am dispatching an action when a function is called , it should try to pass a file/image to another function to upload it to a local folder once done try to make insert into sqlite database , to ensure that these steps will be done one by one , I have used Promises , my issue is that the image is uploaded to local folder but insertion into database fails , would please help to find how I can correct get insertion into database done .

here is my code :

export const addPosts = ( imageName , data,title,categ,idSubCateg) => {
  const idSubCat = idSubCateg ? idSubCateg : 0;
  return (dispatch) => {

    dispatch({ type: ADDING_POST });
    new Promise((resolve, reject) => {   
      uploadImage(imageName, data) // this for upload image to local folder
      const postToSave = { title, imageName };                 
      resolve(registerDataIntoDtabase(dispatch,imageName,title,categ,idSubCat));
      reject(handleError(dispatch, 'an Error Occured'));
    });
  }
}

const registerDataIntoDtabase = (dispatch,title,imagenmae,categ,idSubCat) => {
  //insert into database
  new Promise((resolve, reject) => { 
    db.transaction((tx) => {
        tx.executeSql('INSERT INTO rooms (room,idHouse,title,idOwner) VALUES (?,?,?,?)', [imagenmae,categ,title,idSubCat], (tx, results) => {
          resolve(dispatch({ type: ADDING_SUCCESS }));
          reject(handleError(dispatch, error.message));
        })
    });
  });
}
Andrew
  • 26,706
  • 9
  • 85
  • 101
dsaton22
  • 107
  • 1
  • 7
  • 1
    You might consider using consistent indentation when writing code - it'll make reading and debugging it much easier, not only for potential answerers, but for you as well, when we can all see the `{` `}` blocks and their nesting level at a glance, rather than having to carefully pick through each line just to pick up on the logical paths. – CertainPerformance Feb 14 '19 at 09:47
  • 1
    The issue looks like you are calling both `resolve` and `reject` subsequently, there is not `if/else` or `try/catch` separating them so it won't work – Andrew Feb 14 '19 at 09:56

2 Answers2

-1

you are doing it wrong you must use like this

const SQLite = {
    registerDataIntoDtabase : ()=> new Promise((resolve, reject)=>{

    }),
    addPosts:()=> new Promise((resolve, reject)=>{
        // you code ... 
        SQLite.registerDataIntoDtabase( your_parameters ).then()
    }),
}

in your case function is not called and yes use try catch in your sql query to get some error from sql

Jigar
  • 1,314
  • 9
  • 19
  • It's not clear from your answer what you think the OP is doing wrong or how he should fix it, but it looks like you are suggesting to use the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Feb 14 '19 at 11:03
  • he want to call this registerDataIntoDtabase in redux bounded function which is not possible how can you call a function outside the scope? @Bergi – Jigar Feb 14 '19 at 11:05
-1

Promise-mysql is a wrapper for mysqljs/mysql that wraps function calls with Bluebird promises. Usually this would be done with Bluebird's .promisifyAll() method, but mysqljs/mysql's footprint is different to that of what Bluebird expects.

To install promise-mysql, use npm:

$ npm install promise-mysql Please refer to mysqljs/mysql for documentation on how to use the mysql functions and refer to Bluebird for documentation on Bluebird's promises

At the minute only the standard connection (using .createConnection()) and the pool (using .createPool()) is supported. createPoolCluster is not implemented yet. for more visit https://www.npmjs.com/package/promise-mysql

  • 1
    Please don't just copy the package readme introduction (without properly quoting and citing it), but show how the library could be applied to the specific problem stated in the question. – Bergi Feb 14 '19 at 12:34