将异步函数传递给 onSnapshot
Passing an asynchronous function into onSnapshot
我需要 运行 来自 onSnapshot 的异步函数,但 onSnapshot 本身不是异步函数。如何在收到我的集合已更改的通知后 运行 一个异步函数?有没有办法解决这个问题?
//Adds a listener to the user's offercandidate doc then sets the remote description to what appears in the document
let unsubscribeFromOffer = negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("offer-candidates")
.onSnapshot(() => {
postReturnAnswer();
});
async function postReturnAnswer() {
let doc = await negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("offer-candidates")
.doc("offer")
.get();
let newPeerConnection = new UserConnection(
servers,
doc.data()["offer"]["senderID"]
);
newPeerConnection.userPeerConnection.setRemoteDescription(
doc.data()["offer"]["offer"]
);
let connAnswerDescription =
await peerConnection.userPeerConnection.createAnswer();
await peerConnection.userPeerConnection.setLocalDescription(
connAnswerDescription
);
await negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("answer-candidates")
.add({
answer: JSON.stringify(
peerConnection.userPeerConnection.localDescription
),
});
peerConnections.push(peerConnection);
}
错误信息:
根据上面的代码,您将获得一个关于异步函数的文档。查看 Get a Document,它显示了如何使用 get():
正确检索单个文档的内容
我使用上面的代码设法得到了未定义的错误。使用文档中的代码可以解决您的错误。您可能需要查看以下代码:
let unsubscribeFromOffer = negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("offer-candidates")
.onSnapshot(() => {
postReturnAnswer();
});
async function postReturnAnswer() {
let doc = await negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("offer-candidates")
.doc("offer")
.get()
// This would map the data from the document
.then((doc) => {
// Check if the document exists.
if (doc.exists) {
let newPeerConnection = new UserConnection(
servers,
// On this line, you already accessed on document level so you wont need to call the document again, just call the fieldname.
doc.data().Fieldname
);
newPeerConnection.userPeerConnection.setRemoteDescription(
// Same with this one.
doc.data().Fieldname
);
let connAnswerDescription =
await peerConnection.userPeerConnection.createAnswer();
await peerConnection.userPeerConnection.setLocalDescription(
connAnswerDescription
);
await negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("answer-candidates")
.add({
answer: JSON.stringify(
peerConnection.userPeerConnection.localDescription
),
});
peerConnections.push(peerConnection);
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
}
我需要 运行 来自 onSnapshot 的异步函数,但 onSnapshot 本身不是异步函数。如何在收到我的集合已更改的通知后 运行 一个异步函数?有没有办法解决这个问题?
//Adds a listener to the user's offercandidate doc then sets the remote description to what appears in the document
let unsubscribeFromOffer = negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("offer-candidates")
.onSnapshot(() => {
postReturnAnswer();
});
async function postReturnAnswer() {
let doc = await negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("offer-candidates")
.doc("offer")
.get();
let newPeerConnection = new UserConnection(
servers,
doc.data()["offer"]["senderID"]
);
newPeerConnection.userPeerConnection.setRemoteDescription(
doc.data()["offer"]["offer"]
);
let connAnswerDescription =
await peerConnection.userPeerConnection.createAnswer();
await peerConnection.userPeerConnection.setLocalDescription(
connAnswerDescription
);
await negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("answer-candidates")
.add({
answer: JSON.stringify(
peerConnection.userPeerConnection.localDescription
),
});
peerConnections.push(peerConnection);
}
错误信息:
根据上面的代码,您将获得一个关于异步函数的文档。查看 Get a Document,它显示了如何使用 get():
正确检索单个文档的内容我使用上面的代码设法得到了未定义的错误。使用文档中的代码可以解决您的错误。您可能需要查看以下代码:
let unsubscribeFromOffer = negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("offer-candidates")
.onSnapshot(() => {
postReturnAnswer();
});
async function postReturnAnswer() {
let doc = await negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("offer-candidates")
.doc("offer")
.get()
// This would map the data from the document
.then((doc) => {
// Check if the document exists.
if (doc.exists) {
let newPeerConnection = new UserConnection(
servers,
// On this line, you already accessed on document level so you wont need to call the document again, just call the fieldname.
doc.data().Fieldname
);
newPeerConnection.userPeerConnection.setRemoteDescription(
// Same with this one.
doc.data().Fieldname
);
let connAnswerDescription =
await peerConnection.userPeerConnection.createAnswer();
await peerConnection.userPeerConnection.setLocalDescription(
connAnswerDescription
);
await negDoc
.collection("users")
.doc(sessionStorage.getItem("userID"))
.collection("answer-candidates")
.add({
answer: JSON.stringify(
peerConnection.userPeerConnection.localDescription
),
});
peerConnections.push(peerConnection);
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
}