我正在制作一个与本地数据库密切配合的React和Electron应用程序。我有2张桌子,"被阻止"和#34;解锁"此应用程序的目标是通过单击按钮将一行数据从一个表移动到另一个表。大多数事情似乎工作正常,但我无法使用redux数据异步更新我的表。这是我获取数据的函数:
export function updateBlockedFiles(blacklist_queries) {
console.log(blacklist_queries);
return {
type: UPDATE_BLOCKED_FILES,
payload: blacklist_queries
}
};
export function updateWhitelistedFiles(whitelist_queries) {
console.log(whitelist_queries);
return {
type: UPDATE_WHITELISTED_FILES,
payload: whitelist_queries
}
};
我有2个动作更新redux:
unblockProcess(id) {
window.transferRowFromTable('blacklistdb', 'whitelistdb', id);
var blacklist_queries = window.fetchDBData('blacklistdb');
var whitelist_queries = window.fetchDBData('whitelistdb');
this.props.updateBlockedFiles(blacklist_queries);
this.props.updateWhitelistedFiles(whitelist_queries);
};
我在我的组件中的调用如下所示:
echo off
setlocal EnableDelayedExpansion
SET ImportDirectory=D:\Data Files\
SET MissingFiles=
@REM Build Array of sorts
@set arrayline[0]=%ImportDirectory%File1*.xml
@set arrayline[1]=%ImportDirectory%File2*.xml
@set arrayline[2]=%ImportDirectory%File3*.xml etc
@REM Loop through and check whether file exists
@for /l %%n in (0,1,2) do (
if not exist !arrayline[%%n]! (
echo File does not exist
SET MissingFiles=%MissingFiles%!arrayline[%%n]!
REM echo File !arrayline[%%n]! has not been delivered for processing >> %LogFilePath%
echo File !arrayline[%%n]! has not been delivered for processing
) else (
echo File Exists
)
)
@REM Test to see if any files are missing and send email
IF NOT %MissingFiles% == "" (
@REM Call code that sends the email with Missing Files as the body
)
我发现的问题主要发生在fetchDBData()中。当我调用this.props.updateBlockedFiles()时,它返回一个空的查询数组而不是填充的数组。我认为这可能是一个异步问题,但我不确定如何解决它。我已经看到了使用promises的想法,但我不知道如何将它与React集成。任何帮助都会有所帮助!
答案 0 :(得分:0)
我正在做类似的事情......让我画出概念,看看它是否有意义。
在将IPC请求发送到主进程以运行SQL之前,在渲染器进程中创建一个IPC侦听器,以在SQL查询完成时充当回调。可以构造此回调以处理来自SQLite查询的数据输出。下面的粗略示例:
渲染器流程:
var ipcRenderer = require('electron').ipcRenderer
function handleDBResponse(event, data){
// Do something with the data
}
ipcRenderer.on('dbResponse', handleDBResponse)
ipcRenderer.send('dbQuery', "some_table_name")
主要流程:
var ipcMain = require('electron').ipcMain
function doDBQuery(event, tablename){
// Perform your DB query via SQLite
var db = new sqlite3.Database('processlist.db');
var queries = [];
db.each("SELECT * FROM " + tablename, function(err, row) {
queries.push(row);
});
db.close();
// Send queries back to callback handler
event.sender.send('dbResponse', queries)
}
ipcMain.on('dbQuery', doDBQuery)
这一切都是通过电子的IPC方法在主要和渲染过程之间进行通信来完成的。可以在此处研究更多详细信息:https://github.com/electron/electron/blob/master/docs/api/ipc-main.md