Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取包装函数return中的嵌套参数返回值_Javascript_Reactjs_React Native_Watermelondb - Fatal编程技术网

Javascript 获取包装函数return中的嵌套参数返回值

Javascript 获取包装函数return中的嵌套参数返回值,javascript,reactjs,react-native,watermelondb,Javascript,Reactjs,React Native,Watermelondb,我有一个场景,需要从作为参数传递给另一个函数的函数中获取返回值。我尝试了多种方法。但是无法从ProfileAction.js文件中获取returnValue到CreateProfileComponent // ProfileAction.js export default (database) => { return { createProfile: async (createdProfile) => { const profileCollection = d

我有一个场景,需要从作为参数传递给另一个函数的函数中获取返回值。我尝试了多种方法。但是无法从ProfileAction.js文件中获取returnValueCreateProfileComponent

// 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: "johndoe@gmail.com",
    });
}

最后,我想要的是CreateProfileComponent中的returnValue值。函数database.actions()和profileCollection.create()是从第三方库(WatermelonDB)中使用的。

我不确定
database.action
的作用,但您应该在该函数中返回一个值。类似于以下内容:
return wait database.action(async()=>{

并在捕获时抛出一个错误

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: "johndoe@gmail.com",
        });
    } catch (e) {
    }
}