Unhandled promise rejection: Error: Database Table error: undefined in react native
Unhandled promise rejection: Error: Database Table error: undefined in react native
我正在尝试存储我使用 expo 的 SQLite 为我的 React 本机应用程序创建的数据,但是当我尝试这样做时出现错误。
我的代码:
const savePlaceHandler = async () => {
try {
const newPath = await fileMoveHandler();
const dbResult = await insertData(
titleValue,
newPath,
"Dummy address",
15.2,
16.2
); //Here I am trying to store data in SQLite
console.log("dbResult: ", dbResult); //no logs
dispatch(placesActions.addPlace(titleValue, newPath)); //it doesn't dispatch
props.navigation.goBack();
} catch (err) {
console.log(err);
throw err;
}
};
从上面我试图将数据存储在数据库中。
export const insertData = (title, imageUri, address, lat, lng) => {
const promiseDB = new Promise((resolve, reject) => {
db.transaction((tx) => {
tx.executeSql(
`INSERT INTO places (title, imageUri ,address, lat, lng) VALUES (?,?,?,?,?)`,
[title, imageUri, address, lat, lng],
(error) => {
reject(error);
console.log(error);
throw new Error("Database Table error: " + error.message); //Error
},
(result) => {
resolve(result);
console.log("result: ", result);
}
);
});
});
return promiseDB;
};
根据上面的代码,每当我将数据从 savePlaceHandler
发送到 insertData
时,我都会收到错误消息。
错误:
[Unhandled promise rejection: Error: Database Table error: undefined]
* helper/db.js:34:22 in tx.executeSql$argument_2
- node_modules/@expo/websql/lib/websql/WebSQLTransaction.js:69:13 in self._websqlDatabase._db.exec$argument_2
- node_modules/expo-sqlite/build/SQLite.js:16:8 in SQLiteDatabase#exec
- node_modules/react-native/node_modules/promise/setimmediate/core.js:37:13 in tryCallOne
- node_modules/react-native/node_modules/promise/setimmediate/core.js:123:24 in setImmediate$argument_0
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:130:14 in _callTimer
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:181:14 in _callImmediatesPass
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:441:30 in callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:387:6 in __callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:135:6 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:364:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:134:4 in flushedQueue
* [native code]:null in flushedQueue
* [native code]:null in invokeCallbackAndReturnFlushedQueue
Possible Unhandled Promise Rejection (id: 1):
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": "places.db",
},
"_running": true,
"_txnQueue": Queue {
"first": undefined,
"last": undefined,
"length": 0,
},
"exec": [Function anonymous],
"version": "1.0",
},
}
我无法弄清楚这个错误是什么意思还有为什么我会收到这个?
根据提供的文档 https://docs.expo.dev/versions/latest/sdk/sqlite/#sqltransaction
交换 Transaction.executeSql
的回调实现顺序
export const insertData = (title, imageUri, address, lat, lng) => {
const promiseDB = new Promise((resolve, reject) => {
db.transaction((tx) => {
tx.executeSql(
'INSERT INTO places (title, imageUri ,address, lat, lng) VALUES (?,?,?,?,?)',
[title, imageUri, address, lat, lng],
(tx, result) => {
resolve(result);
},
(tx, error) => {
reject(error);
}
);
});
});
return promiseDB;
};
我正在尝试存储我使用 expo 的 SQLite 为我的 React 本机应用程序创建的数据,但是当我尝试这样做时出现错误。
我的代码:
const savePlaceHandler = async () => {
try {
const newPath = await fileMoveHandler();
const dbResult = await insertData(
titleValue,
newPath,
"Dummy address",
15.2,
16.2
); //Here I am trying to store data in SQLite
console.log("dbResult: ", dbResult); //no logs
dispatch(placesActions.addPlace(titleValue, newPath)); //it doesn't dispatch
props.navigation.goBack();
} catch (err) {
console.log(err);
throw err;
}
};
从上面我试图将数据存储在数据库中。
export const insertData = (title, imageUri, address, lat, lng) => {
const promiseDB = new Promise((resolve, reject) => {
db.transaction((tx) => {
tx.executeSql(
`INSERT INTO places (title, imageUri ,address, lat, lng) VALUES (?,?,?,?,?)`,
[title, imageUri, address, lat, lng],
(error) => {
reject(error);
console.log(error);
throw new Error("Database Table error: " + error.message); //Error
},
(result) => {
resolve(result);
console.log("result: ", result);
}
);
});
});
return promiseDB;
};
根据上面的代码,每当我将数据从 savePlaceHandler
发送到 insertData
时,我都会收到错误消息。
错误:
[Unhandled promise rejection: Error: Database Table error: undefined]
* helper/db.js:34:22 in tx.executeSql$argument_2
- node_modules/@expo/websql/lib/websql/WebSQLTransaction.js:69:13 in self._websqlDatabase._db.exec$argument_2
- node_modules/expo-sqlite/build/SQLite.js:16:8 in SQLiteDatabase#exec
- node_modules/react-native/node_modules/promise/setimmediate/core.js:37:13 in tryCallOne
- node_modules/react-native/node_modules/promise/setimmediate/core.js:123:24 in setImmediate$argument_0
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:130:14 in _callTimer
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:181:14 in _callImmediatesPass
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:441:30 in callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:387:6 in __callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:135:6 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:364:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:134:4 in flushedQueue
* [native code]:null in flushedQueue
* [native code]:null in invokeCallbackAndReturnFlushedQueue
Possible Unhandled Promise Rejection (id: 1):
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": "places.db",
},
"_running": true,
"_txnQueue": Queue {
"first": undefined,
"last": undefined,
"length": 0,
},
"exec": [Function anonymous],
"version": "1.0",
},
}
我无法弄清楚这个错误是什么意思还有为什么我会收到这个?
根据提供的文档 https://docs.expo.dev/versions/latest/sdk/sqlite/#sqltransaction
交换Transaction.executeSql
的回调实现顺序
export const insertData = (title, imageUri, address, lat, lng) => {
const promiseDB = new Promise((resolve, reject) => {
db.transaction((tx) => {
tx.executeSql(
'INSERT INTO places (title, imageUri ,address, lat, lng) VALUES (?,?,?,?,?)',
[title, imageUri, address, lat, lng],
(tx, result) => {
resolve(result);
},
(tx, error) => {
reject(error);
}
);
});
});
return promiseDB;
};