【发布时间】:2021-05-21 18:33:12
【问题描述】:
我有一个场景,我需要从一个函数中获取返回值,该函数作为参数传递给另一个函数。我尝试了多种方法。但无法从 ProfileAction.js 文件中获取 returnValue 到 CreateProfileComponent。
// 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)
【问题讨论】:
标签: javascript reactjs react-native watermelondb