You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron与lowdb使用问题:删除数据时JSON文件操作报错

Troubleshooting lowdb Delete Errors in Electron

Hey there, sorry to hear you're stuck with this deletion issue in your Electron + lowdb setup! Let's walk through the most common culprits and fixes to get your data syncing properly between your table and JSON file.

1. Double-Check Your lowdb File Path (Critical for Electron!)

Electron has different file system contexts for the main process and renderer process, so using relative paths directly can lead to permission issues or missing files. Always store your JSON database in Electron's dedicated userData directory—it's guaranteed to have read/write access.

Here's how to set this up correctly in the main process:

const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const { app } = require('electron');
const path = require('path');

// Get the app's user data directory (cross-platform)
const dbPath = path.join(app.getPath('userData'), 'your-db-file.json');
const adapter = new FileSync(dbPath);
const db = low(adapter);

// Initialize default structure if the file is empty
db.defaults({ tableRows: [] }).write();

If you're handling deletions in the renderer process, don't directly access the file system—use IPC to send a deletion request to the main process, which handles the lowdb operation. This avoids path and permission conflicts.

2. Verify Your Deletion Logic

A common mistake is misusing lowdb's remove method, or forgetting to persist changes with .write(). Let's confirm your deletion code is correct:

Example: Delete by Unique ID

If your rows have a unique id field, your deletion code should look like this:

// In the main process (or via IPC handler)
const deleteRow = (rowId) => {
  // Make sure rowId matches the type in your JSON (number vs string!)
  db.get('tableRows')
    .remove({ id: rowId })
    .write(); // Critical: this saves changes to the JSON file
};
  • If you get an error like Cannot read property 'remove' of undefined, it means db.get('tableRows') is returning undefined. Fix this by ensuring your default database structure (via db.defaults()) includes the tableRows array.
  • Double-check that rowId matches the data type in your JSON (e.g., if your JSON uses numeric IDs, don't pass a string).

3. Handle Async Adapters Properly

If you're using FileAsync instead of FileSync, all lowdb operations are asynchronous. Forgetting to use async/await or .then() will lead to unhandled promise errors:

// Async adapter example
const low = require('lowdb');
const FileAsync = require('lowdb/adapters/FileAsync');

const initDb = async () => {
  const adapter = new FileAsync(dbPath);
  const db = await low(adapter);
  await db.defaults({ tableRows: [] }).write();
  return db;
};

// Deletion with async/await
const deleteRow = async (rowId) => {
  const db = await initDb();
  await db.get('tableRows').remove({ id: rowId }).write();
};

4. Check for Permission Issues

If you're still seeing errors related to file access, confirm you're not trying to write to a read-only directory (like the app's installation folder on Windows/macOS). The userData directory we mentioned earlier is always writable, so stick to that.

Final Tip: Log the Exact Error

If none of the above fixes work, capture the exact error message (e.g., in Electron's dev tools or main process console) and share it—this will make pinpointing the issue much easier. For example:

try {
  db.get('tableRows').remove({ id: rowId }).write();
} catch (err) {
  console.error('Deletion error:', err);
}

内容的提问来源于stack exchange,提问作者Raffaele Garofalo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:23:23