如何解决Mikro ORM + MongoDB + GraphQL 查询嵌入式对象/数组时出错
这可能会有点长,但有很多奇怪的问题需要解释。
"stacktrace": [
"SyntaxError: Arg string terminates parameters early"," at new Function (<anonymous>)"," at Function.createFunction (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/utils/Utils.js:611:52)"," at ObjectHydrator.getEntityHydrator (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/hydration/ObjectHydrator.js:221:40)"," at ObjectHydrator.hydrate (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/hydration/ObjectHydrator.js:23:30)"," at EntityFactory.hydrate (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/entity/EntityFactory.js:82:27)"," at EntityFactory.create (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/entity/EntityFactory.js:36:14)"," at MongoEntityManager.find (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/EntityManager.js:100:52)"," at runMicrotasks (<anonymous>)"," at processticksAndRejections (internal/process/task_queues.js:94:5)"," at async EntityLoader.populateMany (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/entity/EntityLoader.js:127:22)"," at async EntityLoader.populateField (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/entity/EntityLoader.js:187:9)"," at async EntityLoader.populate (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/entity/EntityLoader.js:37:13)"," at async MongoEntityManager.lockAndPopulate (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/EntityManager.js:630:9)"," at async MongoEntityManager.findOne (/Users/zacktidwell/repos/services/graphql/node_modules/@mikro-orm/core/EntityManager.js:244:9)"," at async queryName
]
我 95% 确定此错误是由我设置的嵌入式对象引起的。 下面是一个实体示例:
export class Properties extends BaseEntity {
@Embedded({ entity: () => Address,object: true })
address = new Address();
@property()
market: string;
@Embedded({
entity: () => Metadata,array: true,object: true,nullable: true,})
details: Metadata[] = [];
@Embedded({
entity: () => Metadata,})
features: Metadata[] = [];
constructor({address,market }) {
super();
this.address = address;
this.market = market;
}
}
以及数组的 Embeddable:
@Embeddable()
export class Metadata {
@property()
id?: string;
@property()
MetaKey?: string;
@property()
MetaValue?: string;
@property()
createdAt?: string;
}
我正在绞尽脑汁的奇怪事情:
-
这种情况最常发生在人际关系中。如果它是查询中的主要实体, 错误很少发生。我可以查询一个实体,它可以正常工作,但是如果我 以原始实体作为关系查询其他实体,则不会。
-
它似乎破坏了其他查询。我可以运行一个查询,它会起作用,然后运行一个 不同的查询,它不会。反过来做,同样的事情发生, 第一个查询有效,第二个无效。
-
删除一些嵌入将“修复它”,但哪个并不重要 嵌入式。似乎有更多的嵌入对象破坏了它, 不一定是任何特定的。 (我有十种不同的嵌入, 并且一个实体将有多达八个嵌入式可嵌入项和多达五个 相同的可嵌入不同的领域)
-
我尝试了各种不同的方法来编写或初始化嵌入的对象:
@Embedded({ entity: () => Metadata,}) details = new Metadata(); @Embedded({ entity: () => Metadata,}) details?: Metadata; @Embedded({ entity: () => Metadata) details?: Metadata[]; // and varIoUs combinations of {array: true} and {object: true}
感谢您的建议
解决方法
对于遇到此问题的任何人来说,这实际上是 Mikro ORM 方面的问题。正则表达式拼写错误阻止了实体拥有 10 个以上的可嵌入项。它已被修复;如果您遇到此问题,请更新您的软件包。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。