Getting a nested parameter return value in the wrapped function return

40 views Asked by At

I have a scenario where I need to get the return value from a function that passed to another function as a parameter. I tried multiple ways. But couldn't get the returnValue to the CreateProfileComponent from ProfileAction.js file.

// ProfileAction.js
export default (database) => {
  return {
    createProfile: async (createdProfile) => {
      const profileCollection = database.get("profiles");
      const { name, email } = createdProfile;
      try {
        await database.action(async () => {
          const returnValue = await profileCollection.create((profile) => {
            profile.name = name;
            profile.email = email;
          });
        });
      } catch (error) {
        console.log("createProfile", error);
      }
    },
  };
};

// CreateProfileComponent.js
const CreateProfileComponent = () => {
    const database = useDatabase();
    const profileAction = ProfileAction(database);
    const createdRecord = await profileAction.createProfile({
        name: "John Doe",
        email: "[email protected]",
    });
}

Finally what I want is the returnValue value in CreateProfileComponent. The functions database.actions() and profileCollection.create() are used from a third party library (WatermelonDB)

1

There are 1 answers

0
Zhang TianYu On

I am not sure what database.action does but you should return a value in this function. Like following: return await database.action(async () => {

And throw an error on catch

export default (database) => {
  return {
    createProfile: async (createdProfile) => {
      const profileCollection = database.get("profiles");
      const { name, email } = createdProfile;
      try {
        return await database.action(async () => {
          const returnValue = await profileCollection.create((profile) => {
            profile.name = name;
            profile.email = email;
          });
        });
      } catch (error) {
        console.log("createProfile", error);
        throw error;
      }
    },
  };
};

// CreateProfileComponent.js
const CreateProfileComponent = () => {
    const database = useDatabase();
    const profileAction = ProfileAction(database);
    try {
        const createdRecord = await profileAction.createProfile({
            name: "John Doe",
            email: "[email protected]",
        });
    } catch (e) {
    }
}