"不能使用'in'操作符搜索'0'"的错误,这在localhost上不会发生。

编程入门 行业动态 更新时间:2024-10-12 08:16:17

"不能使用'in'操作符搜索'0'"的错误,<a href=https://www.elefans.com/category/jswz/34/1734907.html style=这在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上不会发生。

本文发布于:2024-05-13 13:03:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这在   错误   发生   操作   localhost

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!