我正在尝试为 rxdb 创建一个插件。我想捕获由引发的异常insert
并返回一个哈希
{[fieldName: string] => [error:string]}
但是,当使用我的新方法时,我遇到了一个异常,似乎该方法是直接在原型上而不是在每个RxColletion<T, T2, T3>
实例上调用的。
我得到的错误是:
TypeError: Cannot read property 'fillObjectWithDefaults' of undefined
因为this.schema
未定义..我在其上运行此方法的集合确实有一个模式..
这是我的插件代码:
export const validatedInsertPlugin: RxPlugin = {
rxdb: true,
prototypes: {
RxCollection(proto: IRxCollectionBaseWithValidatedInsert) {
proto.validatedInsert = async function validatedInsert<T, D>(
doc: T
): Promise<Insert<T>> {
try {
// this is the line that raises:
const product = await proto.insert(doc);
return [true, product];
} catch (e) {
// extract errors
return [false, {} as Errors<T>];
}
};
},
},
overwritable: {},
hooks: {},
};