1

The function I'm coding will be called from other javascript file and the fields and values will be passed to the function including the table name. But when I run the function it just gives error without this error message

    We have encounter an Error WebSQLTransaction { "_complete": false, "_error": null, "_running": true, "_runningTimeout": false, "_sqlQueue": Queue { "first": undefined, "last": undefined, "length": 0, }, "_websqlDatabase": WebSQLDatabase { "_currentTask": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, "_db": SQLiteDatabase { "_closed": false, "_name": "mydb.db", }, "_running": true, "_txnQueue": Queue { "first": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, "next": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, "next": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, }, }, }, "last": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, }, "length": 3, }, "version": "1.0", }, }

So am building this using javascript React native and am using the expo SDK. Expo ships in the SQLite module, Again what am doing is to build a function that can be called from anywhere in the code and it some data into the database.

   export const insert = (tbl, fields, values) =>{
    const query = "insert into ${tbl} (${fields}) values (${values});";
    console.log(query);
    //it looks fine to me
    db.transaction(trx => {
        let trxQuery = trx.executeSql(
           query
        ,[values],(data)=> console.log('we made it',data),(err)=>console.log('We have encounter an Error', err))
        console.log(trxQuery); // retruns undefined
    })
}

This is how I called the function

    const personObj = JSON.parse(personDetails);
        Object.keys(personObj).map(i => 
            insert('users','name, address, hash', [personObj[i].name, personObj[i].address, personObj[i].hash])
        )

I expect to get a console log of we made it and information concerning the data we inserted. Thanks for your assistance

1 Answer 1

3

success and error callback have 2 parameters first one is transaction itself second one is error or resultset. You should edit your code like below and check the resultset object,

`export const insert = (tbl, fields, values) =>{
    const query = "insert into ${tbl} (${fields}) values (${values});";
    console.log(query);
    //it looks fine to me
    db.transaction(trx => {
        let trxQuery = trx.executeSql(
             query
            ,[values]
            ,(transact,resultset) => console.log('we made it',resultset)
            ,(transact,err) => console.log('We have encounter an Error', err)
       );
    })
}`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.