Javascript 异步函数返回未定义的值
我真的需要温习一下我的异步等待和承诺。我想要一些建议 我正在对firebase firestore进行异步函数调用。函数应根据单个输入参数返回字符串 该功能用于1-1用户聊天。 该功能用于创建聊天室/查找现有聊天室,并返回其ID 现在,我得到了Javascript 异步函数返回未定义的值,javascript,reactjs,firebase,promise,async-await,Javascript,Reactjs,Firebase,Promise,Async Await,我真的需要温习一下我的异步等待和承诺。我想要一些建议 我正在对firebase firestore进行异步函数调用。函数应根据单个输入参数返回字符串 该功能用于1-1用户聊天。 该功能用于创建聊天室/查找现有聊天室,并返回其ID 现在,我得到了undefined作为openChat的返回值,我不知道为什么。除返回功能外,该功能还可以工作 我有两个功能。一个是React类组件生命周期方法,另一个是我的firebase异步函数 下面是类组件生命周期方法: async getChatId(userId
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>)
}
如果你有任何有用的建议,我将永远感激听到他们
预期结果是返回的字符串。字符串正确显示在异步函数的console.log中)
实际结果是异步函数的返回值未定义。您不从
openChat
函数返回任何内容,因此该函数解析为未定义
你必须写:
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()
/* .... */
}
而那些getChatId
和requestChat
中的newpromise
没有多大意义。等待openChat(userId)
或this.getChatId(userId)
您应该等待
firestore调用的结果如果您想返回它们的值,您已经在使用异步
函数:
export async function openChat(otherPersonId) {
const user = firebase.auth().currentUser
const userId = user.uid
const doc = await firestore
.collection('users')
.doc(userId)
.get()
let chatsArr = doc.data().chats
let existsArr =
chatsArr &&
chatsArr.filter(chat => chat.otherPersonId === otherPersonId)
if (existsArr && existsArr.length >= 1) {
const theId = existsArr[0].chatId
//update the date, then return id
await firestore
.collection('chats')
.doc(theId)
.update({
date: Date.now(),
})
return theId
} else {
const docRef = await firestore
.collection('chats')
.add({
userIds: { [userId]: true, [otherPersonId]: true },
date: Date.now(),
})
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 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
}
关于:let chatId=wait openChat(userId)
和让result=等待。getChatId(userId)
?此外,您没有从openChat
函数返回任何内容。请参阅
export async function openChat(otherPersonId) {
const user = firebase.auth().currentUser
const userId = user.uid
const doc = await firestore
.collection('users')
.doc(userId)
.get()
let chatsArr = doc.data().chats
let existsArr =
chatsArr &&
chatsArr.filter(chat => chat.otherPersonId === otherPersonId)
if (existsArr && existsArr.length >= 1) {
const theId = existsArr[0].chatId
//update the date, then return id
await firestore
.collection('chats')
.doc(theId)
.update({
date: Date.now(),
})
return theId
} else {
const docRef = await firestore
.collection('chats')
.add({
userIds: { [userId]: true, [otherPersonId]: true },
date: Date.now(),
})
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 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
}