Javascript 获取包装函数return中的嵌套参数返回值
我有一个场景,需要从作为参数传递给另一个函数的函数中获取返回值。我尝试了多种方法。但是无法从ProfileAction.js文件中获取returnValue到CreateProfileComponentJavascript 获取包装函数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
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) {
}
}