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



我有一个场景,需要从一个函数中获取返回值,该函数作为参数传递给另一个函数。我尝试了多种方法。但无法从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(

我不确定database.action做什么,但您应该在这个函数中返回一个值。如下所示:return await 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) {
}
}

最新更新