Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw better error for incorrect config #22

Open
RamiYahyaRY opened this issue May 10, 2019 · 6 comments
Open

Throw better error for incorrect config #22

RamiYahyaRY opened this issue May 10, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@RamiYahyaRY
Copy link

I get this error:

TypeError: Cannot read property 'collection' of undefined
    at /srv/node_modules/integrify/lib/rules/replicateAttributes.js:61:18
    at Array.forEach (<anonymous>)
    at /srv/node_modules/integrify/lib/rules/replicateAttributes.js:47:22
    at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
    at /worker/worker.js:825:24
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

I have the following Firebase Cloud Function running in Node 8 environment with Typescript code:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const { integrify } = require('integrify'); 

admin.initializeApp();
const firestore = admin.firestore();
integrify({ config: { functions, firestore } });

exports.inventory_item_replicate = integrify({
    rule: 'REPLICATE_ATTRIBUTES',
    source: {
      collection: 'inventory_item',
    },
    targets: [
      {
        collection: 'cart',
        foreignKey: 'id',
        attributeMapping: { 
            'barcode': 'barcode',
            'category': 'category', 
            'image': 'image', 
            'name': 'name', 
            'price': 'price',
            'stock': 'stock',
            'type': 'type',
        },
      },
    ],
  }); 

The replication function seems to fire correctly after an update on the original collection, as I can see from the logs, and the error is logged directly afterwards:

integrify: Detected update in [inventory_item], id [QmNKvERKZI3HFEyKz3Ft], 
new value: { ...
...
...
...
integrify: On collection [cart], applying update: { ....
@anishkny
Copy link
Owner

Hey @RamiYahyaRY thanks for trying out integrify!

Could you edit the configuration line thus:

integrify({ config: { functions, db: firestore } });

integrify expects two keys in the config object: functions and db.

LMK if it works. I can enhance it to throw better error message.

@RamiYahyaRY
Copy link
Author

@anishkny Thanks a lot. That works.

Is there a way to bind a collection to a subcollection? Consider this:

  /users/
    userId/
      username
      photoURL

  /articles/
    articleId/
       comments/
          commentId/
             body
             userId       <-- foreign key
             username     <-- replicated field
             photoURL     <-- replicated field

Is there a way to do something like this:

{
    rule: 'REPLICATE_ATTRIBUTES',
    source: {
      collection: 'users',
    },
    targets: [
      {
        collection: 'articles/{articleId}/comments',
        foreignKey: 'userId',
        attributeMapping: { 
            ...
        },
      }
   ]
}

@anishkny anishkny changed the title TypeError: Cannot read property 'collection' of undefined Throw better error for incorrect config May 11, 2019
@anishkny anishkny added the enhancement New feature or request label May 11, 2019
@anishkny
Copy link
Owner

@RamiYahyaRY currently, there isn't a way to replicate to nested collections (subcollections). Although it is on the roadmap as #9.

I will leave this issue open to throw better error messages in case of incorrect config (your original issue).

@anishkny
Copy link
Owner

@RamiYahyaRY you can now replicate to sub-collections using integrify thus:

module.exports.replicateMasterToDetail = integrify({
  rule: 'REPLICATE_ATTRIBUTES',
  source: {
    collection: 'master',
  },
  targets: [
    {
      collection: 'subcollection',
      foreignKey: 'masterId',
      attributeMapping: {
        masterField1: 'detail1Field1',
      },
    },
    isCollectionGroup: true,    // <---- NEWLY ADDED!
  ],

This was added in v2.1.0

@devth
Copy link

devth commented Jun 23, 2020

Can the source be a collection group?

@anishkny
Copy link
Owner

@devth lets continue this discussion at #56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants