【发布时间】:2021-02-12 23:15:33
【问题描述】:
我正在使用 react 构建一个 .html 页面,以方便访问我工作的地方的复杂本地文件系统。这需要任何人都可以编辑。我决定使用 pouchdb 来处理一个数据库,该数据库存储了我所有的按钮以及附加到它们的链接。 这需要部署在多台计算机上。命令 npm run build 工作得很好,但是,按钮数据库不在计算机之间共享,因为 pouchdb 数据库存储在本地存储中。
所以我想出了一种方法来将 pouch 数据库转储到 .json 文件中。 转储过程运行良好,但是当我尝试使用 pouchdb-load 插件从 .json 文件中获取数据时,出现 CORS 错误。
this.db.load('file:///./tracker.json').then(function () {
// done loading!
console.log(this.db)
}).catch(function (err) {
// HTTP error or something like that
console.log(err)
});
我得到一个与 CORS 错误相关的未定义对象:
Access to XMLHttpRequest at 'file:///tracker.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
message: undefined
name: "unknown"
status: 0
当我省略文件路径后面的“file:///”时,我得到以下信息:
yntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at index.js:16
at Array.forEach (<anonymous>)
at parseDump (index.js:12)
at loadString (index.js:32)
at index.js:87
at onSuccess (index-browser.js:288)
at index-browser.js:330
at XMLHttpRequest.xhr.onreadystatechange (index-browser.js:198)
项目需要构建为本地 .html 文件,因为我们需要能够打开文件的链接。 我该怎么做才能使 pouchdb-load 在这样的配置中工作? 我现在很迷茫,所以任何帮助表示赞赏!我究竟做错了什么?是否有任何简单的技巧可以在用户不采取任何操作的情况下从文件系统打开文件?有没有其他方法可以将我的 pouchdb 数据库副本存储在文件系统上?如果是这样,我怎样才能找回它?
【问题讨论】:
标签: javascript json reactjs pouchdb