这在localhost上不会发生。"/>
"不能使用'in'操作符搜索'0'"的错误,这在localhost上不会发生。
我的项目是在 https:/immense-refuge-12167.herokuapp。 如果我登录(邮箱:[email protected],密码:1111111,或者注册后再登录),然后点击左上角的 "浏览器 "按钮,控制台会出现错误,如下图所示。TypeError: Cannot use 'in' operator to search for '0'
只发生在Heroku上,不发生在localhost上。 发生错误的代码是。
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)
}
}
}
},
我意识到这可能是因为 bookmarksOfThisUser
长度为0,所以我用 if(bookmarksOfThisUser.length != 0 && !!bookmarksOfThisUser)
阻止它,但错误仍然发生。该 mounted
功能是填充数据 bookmarks
,默认情况下是一个空数组。使用这些数据的元素是一个 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上发生,而不是在localhost上?
谢谢!我的项目是在https:/immense-refuge-12.html上。
回答如下:这段代码可以检查是否收到了预期的数据响应,然后处理该错误条件。
例如,你可以检查数据类型。
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})
}
更多推荐
"不能使用'in'操作符搜索'0'"的错误,这在localhost上不会发生。
发布评论