我正在编码的函数将从其他javascript文件中调用,并且字段和值将传递给包含表名的函数。但是当我运行该函数时,它只会给出错误而不会显示此错误消息
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", }, }
因此,我要使用JavaScript React native来构建它,并要使用expo SDK。 Expo随SQLite模块一起提供,再次要做的是构建一个可以从代码中的任何位置调用的函数,并将一些数据存储到数据库中。
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
})
}
这就是我所谓的函数
const personObj = JSON.parse(personDetails);
Object.keys(personObj).map(i =>
insert('users','name, address, hash', [personObj[i].name, personObj[i].address, personObj[i].hash])
)
我希望获得we made it
的控制台日志以及有关我们插入的数据的信息。感谢您的协助
答案 0 :(得分:1)
成功和错误回调具有2个参数,第一个是事务本身,第二个是错误或结果集。您应该像下面那样编辑代码并检查结果集对象,
`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)
);
})
}`