当我删除 window.location.href 时,为什么我的代码继续运行?

尹成流
    db.collection("chatroom").where("who", "array-contains-any", [Myuid, Selleruid]).get().then((result) => {
  db.collection('chatroom').add(data)
  window.location.href = "/chat.html"
  })

这是我将聊天室数据库添加到 Firebase 服务器的代码的一部分。它不起作用,但是当我删除 window.location.href = "/chat.html" 时,它可以添加数据。我不明白为什么删除它后它会起作用,以及为什么它不适用于那个窗口 ~ 行。

弗兰克·范·普菲伦

调用add()启动异步写入操作,该操作发生在后台(以防止阻塞浏览器)。但是,您的window.location.href = "/chat.html"语句会立即运行,因此写入操作在完成之前被中止。

要解决此问题,您需要等待写入操作完成后再离开:

db.collection('chatroom').add(data).then(() => {
  window.location.href = "/chat.html"
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

用Sinon存根window.location.href

window.location.href更改时的事件

在JavaScript中模拟window.location.href

“ window.location.href”和“ window.location.hash”之间有什么区别?

Difference between window.location.href and top.location.href

window.location.href = window.location.href和window.location.reload()之间的区别

为什么window.location.href再次追加网址

window.location.href上的onchange事件

如何确保代码在window.location.href之前运行

更改window.location.href时如何还原状态

window.open与window.location.href

更改“ Window.location.href”时,将删除“窗口”对象上存储的属性

Blazor替代JS window.location.href

我正在尝试在循环中使用window.location.href

旧版代码:window.parent.location.href =“ https:/ home”

输入值的onclick window.location.href

当我使用window.location.href时,那么我的另一个函数没有调用。

Window.location.href无限循环

为什么我不能向window.location添加参数?

为什么href =“ javascript:window.location.href ='#xxx';” 不行吗

添加window.location.href时无法插入Firebase

window.location.href无法正常工作

loc = $('<a>',{href:window.location})[0]是什么?吝啬的?

window.location.href 未被修改

为什么我的“window.location =”行不起作用?

即使 Window.location.href 包含查询字符串,为什么 Window.location.search 仍未定义?

window.location.href 和 <a href></a> 之间有什么区别?

为什么即使在删除 window.location("url") 后我也会被重定向

抓取 window.location.href 链接