javascript - 如何从承诺中获取数据?
问题描述
我是新的承诺,并且被卡住了。我想在最后获取数据,并返回一个承诺,因为我正在编写一个函数,我将在某些情况下使用它并对返回的数据做不同的事情。
我想按.then((user) => Promise.resolve({ user, userObject }))
原样返回 LAST ,以便在我将使用函数的地方与 then 一起使用它,但现在我不会以该语句结束,以防我在第一个 .then 语句中丢失数据。怎么了?
更新:
所以这里是完整的功能体。现在我像这样使用它:
signUpUser({ credentials: preparedForm })
.then((t) => console.log('submitHandler -> t', t))
.catch((e) => {
handleSignUpErrors({
form: signUpForm,
updateForm: setSignUpForm,
error: e,
showErrors: setShowErrors,
})
})
但我没有得到console.log。怎么了?
const signUpUser = ({ credentials }) => {
const {
name,
lastName,
email,
password,
phone,
emailVerified,
phoneVerified,
} = credentials
const userObject = getUserObject({
name,
lastName,
email,
password,
phone,
emailVerified,
phoneVerified,
})
return firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then((data) =>
db
.collection('users')
.doc(data.user.uid)
.set(userObject)
.then(() => data)
)
.then((user) => ({ user, userObject }))
}
解决方案
const userObject = getUserObject({
name,
lastName,
email,
password,
phone,
emailVerified,
phoneVerified,
})
return firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then((data) =>
db
.collection('users')
.doc(data.user.uid)
.set(userObject)
.then(response => response)
)
.then(result => console.log(result))
推荐阅读
- python - How to not see "None" output when getting user input(): input(print("Your key:"))
- sql - 在 SQL Server 中将 XML UTF-8 编码字符串转换为 XML 数据类型
- javascript - StencilJS 在 HTML 文件中使用组件
- python - 无法在 Pythonanywhere 上使用 pyvirtualdisplay + selenium + firefox 下载 csv
- python - 如何在pytorch中修改张量数据维度,谢谢
- c - 如果文件具有重音字符(例如“á”),我如何从 C 中读取文件?
- angular - 如何通过转义关闭 Angular Material Sidenav?
- c++ - C++ std::string 初始化性能更好(汇编)
- angular - 运行 cypress 测试时,Angular 应用程序未检测到更改
- jquery - '响应式菜单 jquery html 和 css