javascript - 异步函数返回未定义
问题描述
我真的需要重温我的异步等待和承诺。我会喜欢一些建议。
我正在对 firebase firestore 进行异步函数调用。该函数应根据单个输入参数返回一个字符串。
该功能适用于 1-1 用户聊天。该功能是创建聊天/查找现有聊天,并返回其 ID。
现在,我得到undefined
了返回值,openChat
但不知道为什么。除了返回之外,该函数还可以正常工作。
我有两个功能。一个是 React 类组件生命周期方法,另一个是我的 firebase 异步函数。
这是类组件生命周期方法:
async getChatId(userId) {
let chatPromise = new Promise((resolve, reject) => {
resolve(openChat(userId))
})
let chatId = await chatPromise
console.log('chatId', chatId) //UNDEFINED
return chatId
}
async requestChat(userId) {
let getAChat = new Promise((resolve, reject) => {
resolve(this.getChatId(userId))
})
let result = await getAChat
console.log('result', result) //UNDEFINED
}
render() {
return (<button onClick = {() => this.requestChat(userId)}>get id</button>)
}
这是异步功能:
// both my console.log calls show correctly in console
// indicating that the return value is correct (?)
export async function openChat(otherPersonId) {
const user = firebase.auth().currentUser
const userId = user.uid
firestore
.collection('users')
.doc(userId)
.get()
.then(doc => {
let chatsArr = doc.data().chats
let existsArr =
chatsArr &&
chatsArr.filter(chat => {
return chat.otherPersonId === otherPersonId
})
if (existsArr && existsArr.length >= 1) {
const theId = existsArr[0].chatId
//update the date, then return id
return firestore
.collection('chats')
.doc(theId)
.update({
date: Date.now(),
})
.then(() => {
console.log('existing chat returned', theId)
//we're done, we just need the chat id
return theId
})
} else {
//no chat, create one
//add new chat to chats collection
return firestore
.collection('chats')
.add({
userIds: {
[userId]: true,
[otherPersonId]: true
},
date: Date.now(),
})
.then(docRef => {
//add new chat to my user document
const chatInfoMine = {
chatId: docRef.id,
otherPersonId: otherPersonId,
}
//add chat info to my user doc
firestore
.collection('users')
.doc(userId)
.update({
chats: firebase.firestore.FieldValue.arrayUnion(chatInfoMine),
})
//add new chat to other chat user document
const chatInfoOther = {
chatId: docRef.id,
otherPersonId: userId,
}
firestore
.collection('users')
.doc(otherPersonId)
.update({
chats: firebase.firestore.FieldValue.arrayUnion(chatInfoOther),
})
console.log('final return new chat id', docRef.id)
return docRef.id
})
}
})
}
如果您有任何有用的提示,我将永远感激听到它们!
预期结果是返回的字符串。该字符串正确显示在 async 函数的 console.log 中)。
实际结果是异步函数的返回值是未定义的。
解决方案
您不会从openChat
函数中返回任何内容,因此该函数解析为undefined
.
你必须写:
export async function openChat(otherPersonId) {
const user = firebase.auth().currentUser
const userId = user.uid
return firestore // here you need to return the returned promise of the promise chain
.collection('users')
.doc(userId)
.get()
/* .... */
}
而那些new Promise
并没有多大意义。等待或的结果就足够了getChatId
requestChat
openChat(userId)
this.getChatId(userId)
async getChatId(userId) {
let chatId = await openChat(userId)
console.log('chatId', chatId) //UNDEFINED
return chatId
}
async requestChat(userId) {
let result = await this.getChatId(userId)
console.log('result', result) //UNDEFINED
}
推荐阅读
- react-native - react-native 上的 web3:错误:返回错误:gas 资金不足 * 价格 + 价值
- php - 如何检查请求何时发送
- c++ - 如何在派生类上使用初始化列表?
- python - 如何在 Python 中使用用户的输入调用方法
- reactjs - 一个功能组件内部可以有多个功能吗?
- excel - 将多个文件从不同文件夹复制到不同的目标文件夹
- reactjs - react.js 设置默认路由
- r - eval(substitute(x), data, parent.frame()) 中的错误:缺少参数“data”,没有默认值
- php - 如何显示和分组帖子?
- ios - 将 UIVisualEffectView 添加到 UIView 大小未知的部分