Javascript “错误”;无法使用';在';操作员搜索';0'&引用;,它不';不会在本地主机上发生
我的项目正在进行中Javascript “错误”;无法使用';在';操作员搜索';0'&引用;,它不';不会在本地主机上发生,javascript,node.js,vue.js,webpack,vuetify.js,Javascript,Node.js,Vue.js,Webpack,Vuetify.js,我的项目正在进行中 如果我登录(电子邮件:a@a.com,密码:11111111,或先注册后登录),然后点击左上角的“浏览器”按钮,控制台会出现如下错误: TypeError:无法使用“in”运算符搜索“0” 它只发生在Heroku上,而不发生在本地主机上。 发生错误的代码是: async mounted() { if(this.$store.state.isUserLoggedIn){ const bookmarksOfThisUser
如果我登录(电子邮件:a@a.com,密码:11111111,或先注册后登录),然后点击左上角的“浏览器”按钮,控制台会出现如下错误:
TypeError:无法使用“in”运算符搜索“0”
它只发生在Heroku上,而不发生在本地主机上。
发生错误的代码是:
async mounted() {
if(this.$store.state.isUserLoggedIn){
const bookmarksOfThisUser = (await api.getAllBookmarks(this.$store.state.user.id)).data;
if(bookmarksOfThisUser.length != 0 && !!bookmarksOfThisUser){
for(var bookmark in bookmarksOfThisUser){
const song = (await api.getASong(bookmarksOfThisUser[bookmark]['songId'])).data;
this.bookmarks.push(song)
}
}
}
},
我意识到这可能是因为
bookmarksofhisuser
的长度为0,所以我使用if(bookmarksofhisuser.length!=0&&!!bookmarksofhisuser)
来阻止它,但错误仍然发生。
mounted
功能用于填充数据书签
,默认情况下,书签为空数组。使用此数据的元素是v-data-table
,如下所示
<v-data-table
:headers="headers"
:items="bookmarks"
:items-per-page="5"
class="elevation-1"
v-if="this.bookmarks.length != 0"
>
<template v-slot:item.action="item">
<v-btn class="primary" small v-bind:to="{name: 'viewSong', params:{
songId: item.item.id
}}">View</v-btn>
</template>
</v-data-table>
看法
我还用v-if
检查了它的长度,但仍然出现了错误。。。
有人能给我一些关于我哪里做错了的提示吗?为什么它只发生在Heroku上而不是本地主机上?
谢谢大家! 此代码可以检查它是否收到预期的数据响应,然后处理该错误情况 例如,您可以检查数据类型:
const response = await api.getAllBookmarks(this.$store.state.user.id);
const bookmarksOfThisUser = response.data;
if (Array.isArray(bookmarksOfThisUser)) {
for(var bookmark in bookmarksOfThisUser){
const song = (await api.getASong(bookmarksOfThisUser[bookmark]['songId'])).data;
this.bookmarks.push(song)
}
} else {
// example only of logging the data that is not as expected
console.log({response})
console.log({bookmarksOfThisUser})
}
bookmarksofhisuser
应该是对象而不是数组,因此bookmarksofhisuser.length!=0
不重要。相反,要执行if(Object.keys(bookmarksofhisuser.length)
也可以加载html内容https://immense-refuge-12167.herokuapp.com/allBookmarks?userId=2
@NikosM.Sry,但错误仍然发生。请查看本文中的heroku链接。登录然后转到“浏览器”,错误将显示在控制台中。我已经按照您的指示修改了我的代码,并将其推给了Heroko。显然,我指的是一个错误的条件,但您的问题没有发生,因为bookmarksofthis用户
的长度为零,这是另一个原因。我只是纠正了你的条件测试,但这不是问题的原因。你的变量很可能是一个字符串。谢谢。使用您的代码,我发现这是因为我的数据库不工作,所以返回的数据从来不是数组。很高兴听到它帮助您找到了核心问题!