Javascript jQuery函数在Promise中不可用。然后()
我在我的网站上实现了一个带线程的评论板 这是我的Javascript jQuery函数在Promise中不可用。然后(),javascript,jquery,promise,Javascript,Jquery,Promise,我在我的网站上实现了一个带线程的评论板 这是我的index.html的一部分,其中注释数据从Firebase下载并通过库显示在前端 在一个实验中,我尝试在一个元素中显示一个注释板(例如,body)。 在下面的第一段代码中,它不会抛出任何错误。但是,这不是调用评论板函数的正确位置,因为该函数必须使用来自Firebase的数据 因此,我将jQuery函数移动到然后db.collection(第二个代码)的方法,以便注释函数可以从Firebase(存储在querySnapshot中)获取所有数据 然后
index.html
的一部分,其中注释数据从Firebase下载并通过库显示在前端
在一个实验中,我尝试在一个元素中显示一个注释板(例如,body
)。
在下面的第一段代码中,它不会抛出任何错误。但是,这不是调用评论板函数的正确位置,因为该函数必须使用来自Firebase的数据
因此,我将jQuery函数移动到然后db.collection
(第二个代码)的方法,以便注释函数可以从Firebase(存储在querySnapshot
中)获取所有数据
然后第二个抛出错误
未捕获(承诺中)TypeError:$(…)。注释不是函数
为什么会这样?jQuery函数如何正确使用Firebase中的数据
第一代码
<script>
window.onload = () => {
$("body").comments({
profilePictureURL: 'sample-icon.png',
getComments: (success, error) => {
var commentsArray = [{
...
}];
success(commentsArray);
}
});
const db = firebase.firestore()
db.collection("projects").get().then(querySnapshot => {
...
})
}
</script>
window.onload=()=>{
$(“正文”)。评论({
profilePictureURL:'sample icon.png',
getComments:(成功,错误)=>{
var commentsArray=[{
...
}];
成功(评论雷);
}
});
const db=firebase.firestore()
db.collection(“projects”).get().then(querySnapshot=>{
...
})
}
第二代码
<script>
window.onload = () => {
const db = firebase.firestore()
db.collection("projects").get().then(querySnapshot => {
$("body").comments({
profilePictureURL: 'sample-icon.png',
getComments: (success, error) => {
var commentsArray = [{
...
}];
success(commentsArray);
}
});
})
}
</script>
window.onload=()=>{
const db=firebase.firestore()
db.collection(“projects”).get().then(querySnapshot=>{
$(“正文”)。评论({
profilePictureURL:'sample icon.png',
getComments:(成功,错误)=>{
var commentsArray=[{
...
}];
成功(评论雷);
}
});
})
}
这是我网站文件结构的一部分
/public <--- Hosting on Firebase
index.html
/forum
index.html <--- the index.html shown in the question above
/css
jquery-comment.css <--- from the jQuery library
/js
jquery-comment.js <--- from the jQuery library
/public
window.onload=()=>{
const db=firebase.firestore()
//使用闭包
常数jq=$;
db.collection(“projects”).get().then(querySnapshot=>{
jq(“机构”)。评论({
profilePictureURL:'sample icon.png',
getComments:(成功,错误)=>{
var commentsArray=[{
...
}];
成功(评论雷);
}
});
})
}
谢谢。成功了!你能给我一些关于这个问题的信息吗?问题的原因是什么(如范围等)?非常感谢您提供任何文档或链接。这个解决方案看起来简单漂亮,但我还是不太明白。这个解决方案掩盖了这个问题。有关实际发生的情况的解释,请参见上面的注释。在您显示的代码中,没有理由抛出“$(…).comments()”。在代码中的其他地方可能会出现以下情况:(a)注释插件从jQuery中卸载,或者(b)$
成员被另一个jQuery实例覆盖,而该实例没有安装注释插件。无论哪种方式,当.then()
回调触发时(在稍后的事件线程中),$
(即jQuery)没有注释
方法。