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

Use withConverter together with bindFirestoreRef for firestoreAction #608

Closed
sceee opened this issue Feb 20, 2020 · 3 comments
Closed

Use withConverter together with bindFirestoreRef for firestoreAction #608

sceee opened this issue Feb 20, 2020 · 3 comments
Labels

Comments

@sceee
Copy link

sceee commented Feb 20, 2020

Is it possible to use withConverter to convert streaming data from Firestore directly within a firestoreAction action so that objects can be transformed/converted to custom types?

I tried it but the issue is that bindFirestoreRef(..) requires a firestore.CollectionReference<firestore.DocumentData> | firestore.Query<firestore.DocumentData> as second parameter (the query/Firestore reference). So you cannot just append .withConverter(...) to the query that is handed to the bindFirestoreRef(...).

Is this possible somehow?

@taylorhoward92
Copy link

taylorhoward92 commented Aug 18, 2020

I've been able to use withConverter. I'm using vuex-module-decorators so my syntax might be different to yours.

import * as schema from '@vendida/schema';
import { Action, Module, VuexModule } from 'vuex-module-decorators';
import firebaseApp from '../../utils/firebase';
import { firestoreAction } from 'vuexfire';

@Module({ name: 'Categories' })
export default class Categories extends VuexModule {

  categories: schema.models.Category[] = [];

  @Action
  bindCategories() {
    return (firestoreAction(
      ({ bindFirestoreRef }) =>
        bindFirestoreRef(
          schema.models.Category.COLLECTION_NAME,
          firebaseApp
            .firestore()
            .collection(schema.models.Category.COLLECTION_NAME)
            .withConverter(schema.models.Category.getFirestoreConverter())
        )
    ) as Function)(this.context);
  }

  @Action
  unbindCategories() {
    return (firestoreAction(
      ({ unbindFirestoreRef }) =>
        unbindFirestoreRef(schema.models.Category.COLLECTION_NAME)
    ) as Function)(this.context);
  }

}

@Chippd
Copy link

Chippd commented Jun 7, 2021

This is the second time I've arrived here looking for this answer, so for future Chris, here you go, past Chris got it working.

bindUserData: firestoreAction(async ({ bindFirestoreRef }, uid) => {

		try {
			let bound = await bindFirestoreRef('userData',
				db.collection('users')
				.withConverter(helpers.userDataConverter)
				.doc(uid)
			);
		console.log('user data bound to id:', uid)
		return bound
		} catch(e){
			console.log('binding error:', e)
			return new Error(e)
		}
}),

@posva posva mentioned this issue Oct 13, 2022
43 tasks
posva added a commit that referenced this issue Oct 19, 2022
Close #608

BREAKING CHANGE: `options.serialize()` is replaced with `converter`. It
effectively has the same effect as calling `doc().withConverter()` or
`collection().withConverter()` but it allows to have a global converter
that is automatically applied to all snapshots. This custom converter
adds a non-enumerable `id` property for documents like the previous
`serialize` options. **If you were not using this option**, you don't
need to change anything.
@posva
Copy link
Member

posva commented Oct 19, 2022

Implemented in 18224e4 (might still change and feedback welcome once it's released)

@posva posva closed this as completed Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants