benwinding / react-admin-firebase

A firebase data provider for the react-admin framework
https://benwinding.github.io/react-admin-firebase/
MIT License
460 stars 175 forks source link

Unable to create post #258

Closed shidevil closed 1 year ago

shidevil commented 1 year ago

Hi everyone,

I just want to check so there seems to be an issue with the create post, and it can't be created. I have not touched any code yet.

  1. Git clone
  2. Put in firebase config (check on rule)
  3. yarn
  4. yarn start-demo

I would appreciate any help!

Screenshot 2023-01-27 at 4 23 35 PM
shidevil commented 1 year ago

Tested in the sample - https://benwinding.github.io/react-admin-firebase/

Did not work as well

Georgi-Slavchev commented 1 year ago

I had different error but the same problem. If you are using the lib without authentication fireWrapper.authGetUserLoggedIn() returns a rejection which is uncaught and breaks the flow. @benwinding might be worth looking into this.

shidevil commented 1 year ago

@Georgi-Slavchev did you managed to solve ur issue?

Georgi-Slavchev commented 1 year ago

Yes sorry, I was not very clear on that. Inside the ResourceManager.ts there are two functions: getCurrentUserEmail, getCurrentUserId. I replaced the if - else with try catch where in the catch I just return the logic from the else.

shidevil commented 1 year ago

could you share the code @Georgi-Slavchev and probably can do a pull request if it’s broken.

KonkretneKosteczki commented 1 year ago

I had the same issue and resolved it by simply filtering out the undefined fields.

When you initialize the dataprovider you can pass the transformToDb option to it.

import {FirebaseDataProvider} from "react-admin-firebase";

const config = {
  apiKey: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  authDomain: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  databaseURL: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  projectId: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  storageBucket: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  messagingSenderId: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
};
const dataProvider = FirebaseDataProvider(config, {
  app: firebaseAppInstance,
  transformToDb: (resourceName, documentData) => {
      // remove undefined fields from the document, since firestore does not accept undefined as a field value
      return Object.fromEntries(
          Object.entries(documentData).filter(([, val]) => val !== undefined)
      );
  }
});

It's not a catch all clause because it would not remove undefineds for nested objects, but for your purpose it seems satisfactory.

It is a solution assuming you do not want the fields to be added, but if you still want the fields in the result, instead of filtering out the field, you could map them to nulls or better yet set default values for form inputs to nulls.

It's a little problematic implementing it recursively because certain class objects like for instance firebase Timestamp from datefield, would turn into an object instead of remaining a class.

tommylenz commented 1 year ago

I just ran into the same issue as well, I think this is still a breaking issue if you are not using authentication. @benwinding and the rest, do you know whether someone is on this? If not, I could take a look (but cannot promise to deliver a solution as I am short on time right now). Would you mind reopening the issue, or should I create a new one?