首页 > 解决方案 > PouchDB 具有“未定义”的文档,而不仅仅是 {} 或 {_id}。如何删除它们?

问题描述

我的pouchdb-find查询失败是因为有一些错误的数据对象:它们undefined实际上存储undefined在数据库中。它们不是{}或 { _id: 'something'},它们实际上是undefineds。

细节

我在尝试查询非空字段时发现了它。当我查询{$eq: null}查找这些记录时,我发现了一些奇怪的东西。

询问:

await db.find({ selector: {text: {$gt: null}} });
// or
await db.find({ selector: {text: {$exists: true}} });

结果:

{
  docs: [
    undefined,
    undefined,
    undefined,
    undefined,
    {
      _id: 'test-id-1618443059797',
      _rev: '3-251568304e939d8d5b688af8c09eaa13'
    },
    {
      _id: 'test-id-1618443082960',
      _rev: '3-a60c1dde0e11e14d0a14cd86d2478140'
    }
  ]
}

这似乎可以解释为什么我会收到如下错误。(格式略有不同来自tslog)。

TypeError  Cannot read property 'text' of undefined
error stack:
• index.js:14 getFieldFromDoc
    node_modules/.pnpm/pouchdb-selector-core@7.2.2/node_modules/pouchdb-selector-core/lib/index.js:14:18

• index.js:345 <anonymous>
    node_modules/.pnpm/pouchdb-selector-core@7.2.2/node_modules/pouchdb-selector-core/lib/index.js:345:25

• index.js:342 rowFilter
    node_modules/.pnpm/pouchdb-selector-core@7.2.2/node_modules/pouchdb-selector-core/lib/index.js:342:25

• index.js:319 <anonymous>
    node_modules/.pnpm/pouchdb-selector-core@7.2.2/node_modules/pouchdb-selector-core/lib/index.js:319:12

• index.js:318 filterInMemoryFields
    node_modules/.pnpm/pouchdb-selector-core@7.2.2/node_modules/pouchdb-selector-core/lib/index.js:318:15

• index.js:1307 <anonymous>
    node_modules/.pnpm/pouchdb-find@7.2.2/node_modules/pouchdb-find/lib/index.js:1307:40

其他涉及字段的查询也会发生同样的错误,因为null.<anything>会不断导致这样的错误。


我认为正在发生的事情

我认为这些是损坏的写入,我试图弄清楚如何在不执行destroy(). undefined该州总体上看起来很腐败,因为这些记录有多个。

我试过的

环境

我目前的使用是在 nodejs 环境中。执行通过,ts-node但我怀疑这很重要。

依赖版本

pouchdb 7.2.2
pouchdb-find 7.2.2

如何删除这些undefined文件?

除了破坏,还有什么办法可以解决吗?(意思是,我会将数据复制出来,销毁,然后将所有未损坏的数据放回去;但这可能会导致新问题

标签: javascriptnode.jstypescriptcouchdbpouchdb

解决方案


推荐阅读