当使用 sqlite3 模块中的db.serialize()
(sqlite3 on npm)时,访问对象属性会出错,这不是' db之外的问题。 serialize()`call。
有问题的代码:
var files = [];
var fragments = {};
// [...]
function massInsertIntoDB() {
// This works fine...
console.log('Files length: ', files.length);
console.log('Fragments length: ', Object.keys(fragments).length);
db.serialize(() => {
// Still works fine
console.log('Files length: ', files.length);
// -----------------
// ERROR
// -----------------
console.log('Fragments length: ', Object.keys(fragments).length);
// [...]
});
}
控制台输出:
Files length: 292582
Fragments length: 20242
Files length: 292582
/Users/Jonas/dev/electron-projects/tag-file-search/file-search/sqlite-insert-and-search.js:78
console.log('Fragments length: ', Object.keys(fragments).length);
^
TypeError: Cannot convert undefined or null to object
at Function.keys (native)
at Database.<anonymous> (/Users/Jonas/dev/electron-projects/tag-file-search/file-search/sqlite-insert-and-search.js:78:46)
at massInsertIntoDB (/Users/Jonas/dev/electron-projects/tag-file-search/file-search/sqlite-insert-and-search.js:75:6)
at Interface.<anonymous> (/Users/Jonas/dev/electron-projects/tag-file-search/file-search/sqlite-insert-and-search.js:195:3)
at emitNone (events.js:72:20)
at Interface.emit (events.js:166:7)
at Interface.close (readline.js:285:8)
at ReadStream.onend (readline.js:94:10)
at emitNone (events.js:72:20)
at ReadStream.emit (events.js:166:7)
我不知道为什么这不起作用,我们将非常感谢任何帮助!