0

I am trying to create a plugin for rxdb. I want to catch the exception raised by insert and return an hash with {[fieldName: string] => [error:string]}

When using my new method though, I am getting an exception, and it seems like the method is getting called directly on the prototype rather than on each RxColletion<T, T2, T3> instance.

The error i am getting is:

TypeError: Cannot read property 'fillObjectWithDefaults' of undefined

which happens here: https://github.com/pubkey/rxdb/blob/ac9fc95b0eda276110f371afca985f949275c3f1/src/rx-collection.ts#L443

because this.schema is undefined.. The collection I am running this method on does have a schema though..

Here is my plugin code:

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: {},
};
Shiyason
  • 759
  • 7
  • 16

1 Answers1

1

To answer my own question,

proto.insert is targeting the prototype, which is not what I want. function(this: RxCollection) is what I want. I have to use this which will target the actual instance.

proto.validatedInsert = async function validatedInsert<T1>(
                this: RxCollection,
                doc: T1
            ): Promise<ValidatedInsert<T1>> {
                try {
                    const product = await this.insert(doc); // this, not proto
                    return [true, product];
                } catch (e) {
    ...

Shiyason
  • 759
  • 7
  • 16