VUE: Error `Cannot use 'in' operator to search for 'path' in undefined`; infinite loop; did check similiar question on Stackoverflow. github,web below

huangapple go评论76阅读模式
英文:

VUE: Error `Cannot use 'in' operator to search for 'path' in undefined`; infinite loop; did check similiar question on Stackoverflow. github,web below

问题

这只是我的学习演示,您可以在我的GitHub上查看完整的代码:https://github.com/tNhanDerivative/nhanStore_frontEnd,以及我的域名:vuestore.nhan-thenoobmaster.com
我在本地机器上看不到这个错误,但当我在服务器上部署我的应用程序时,就会出现这个问题。当我尝试运行 npm run serve 时,这些错误也不会出现,只有在从服务器上提供的构建文件 dist/ 时才会出现。

以下是我的 router/index.js 文件。我尝试将 history: createWebHistory(process.env.BASE_URL) 更改为 history: createWebHistory(),但没有效果。我还检查了所有的 router-link,它们都有 to 属性。

import { createRouter, createWebHistory } from 'vue-router';
import store from '../store';
import HomeView from '../views/HomeView.vue';
import ProductView from '../views/ProductView.vue';
import CategoryView from '../views/CategoryView.vue';
import SearchView from '../views/SearchView.vue';
import CartView from '../views/CartView.vue';
import SignUpView from '../views/SignUpView.vue';
import LogInView from '../views/LogInView.vue';
import MyAccountView from '../views/MyAccountView.vue';
import CheckoutView from '../views/CheckoutView.vue';
import SuccessView from '../views/SuccessView.vue';

const routes = [
  {
    path: '/',
    name: 'Home',
    component: HomeView
  },
  // ... 其他路由配置 ...
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireLogin) && !store.state.isAuthenticated) {
    next({ name: 'LogIn', query: { to: to.path } });
  } else {
    next();
  }
});

export default router;

此外,出现了无限循环的问题,是因为 HomeView.vue 中使用的 ProductBox 组件。在 mounted 钩子中进行了 axios API 调用,更新了 latestProducts 数据,导致 ProductBox 重新渲染。

以下是在哪里使用该组件的代码:
HomeView.vue

<template>
  <div class="home">
    <section class="hero is-medium is-dark mb-6">
        <div class="hero-body has-text-centered">
            <p class="title mb-6">
                Welcome to NoobStore
            </p>
            <p class="subtitle">
                The most honest store online don't sell you anything
            </p>
        </div>
    </section>

    <div class="columns is-multiline">
      <div class="column is-12">
          <h2 class="is-size-2 has-text-centered">Latest products</h2>
      </div>

      <ProductBox 
        v-for="product in latestProducts"
        v-bind:key="product.id"
        v-bind:product="product" />
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import ProductBox from '@/components/ProductBox';

export default {
  name: 'HomeView',
  data() {
    return {
      latestProducts: []
    }
  },
  components: {
    ProductBox
  },
  mounted() {
    this.getLatestProducts()
    document.title = 'Nhan`s Store | Vuejs demo';
  },
  methods: {
    async getLatestProducts() {
      this.$store.commit('setIsLoading', true)
      try {
        const res = await axios.get('/api/v1/latest-products/')
        this.latestProducts = res.data
      } catch(error) {
        console.log(error)
      }
      this.$store.commit('setIsLoading', false)
    }
  }
}
</script>

希望这些信息对您有帮助。如果您有其他问题,请随时提出。

英文:

This is just my demo for learning purpose you can look at my full code at github: https://github.com/tNhanDerivative/nhanStore_frontEnd and my domain: vuestore.nhan-thenoobmaster.com
I don't see this error at my local machine but when I deploy my app on server. This happen. When I try to serve npm run serve these Error also does not appear. Only appear when I serve built file dist/ from my server

Cannot use &#39;in&#39; operator to search for &#39;path&#39; in undefined
    at Object.j [as resolve] (vue-router.mjs:3015:13)
    at w.fn (vue-router.mjs:2157:41)
    at w.run (reactivity.esm-bundler.js:190:25)
    at get value [as value] (reactivity.esm-bundler.js:1171:39)
    at w.fn (vue-router.mjs:2159:35)
    at w.run (reactivity.esm-bundler.js:190:25)
    at get value [as value] (reactivity.esm-bundler.js:1171:39)
    at w.fn (vue-router.mjs:2184:60)
    at w.run (reactivity.esm-bundler.js:190:25)
    at get value [as value] (reactivity.esm-bundler.js:1171:39)

Here is my router/index.js file. I have tried change history: createWebHistory(process.env.BASE_URL) to history: createWebHistory(). It does nothing
I also checked all the router-link all has to property

import { createRouter, createWebHistory } from &#39;vue-router&#39;

import store from &#39;../store&#39;

import HomeView from &#39;../views/HomeView.vue&#39;


import ProductView from &#39;../views/ProductView.vue&#39;
import CategoryView from &#39;../views/CategoryView.vue&#39;
import SearchView from &#39;../views/SearchView.vue&#39;
import CartView from &#39;../views/CartView.vue&#39;
import SignUpView from &#39;../views/SignUpView.vue&#39;
import LogInView from &#39;../views/LogInView.vue&#39;
import MyAccountView from &#39;../views/MyAccountView.vue&#39;
import CheckoutView from &#39;../views/CheckoutView.vue&#39;
import SuccessView from &#39;../views/SuccessView.vue&#39;



  const routes = [
  {
    path: &#39;/&#39;,
    name: &#39;Home&#39;,
    component: HomeView
  },
  {
    path: &#39;/about&#39;,
    name: &#39;AboutView&#39;,
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ &#39;../views/AboutView.vue&#39;)
  },
  {
    path: &#39;/sign-up&#39;,
    name: &#39;SignUp&#39;,
    component: SignUpView
  },
  {
    path: &#39;/log-in&#39;,
    name: &#39;LogIn&#39;,
    component: LogInView
  },
  {
    path: &#39;/my-account&#39;,
    name: &#39;MyAccount&#39;,
    component: MyAccountView,
    meta: {
        requireLogin: true
    }
  },
  {
    path: &#39;/search&#39;,
    name: &#39;Search&#39;,
    component: SearchView
  },
  {
    path: &#39;/cart&#39;,
    name: &#39;Cart&#39;,
    component: CartView
  },
  {
    path: &#39;/cart/success&#39;,
    name: &#39;SuccessView&#39;,
    component: SuccessView
  },
  {
    path: &#39;/cart/checkout&#39;,
    name: &#39;CheckoutView&#39;,
    component: CheckoutView,
    meta: {
        requireLogin: true
    }
  },
  {
    path: &#39;/:category_slug/:product_slug&#39;,
    name: &#39;Product&#39;,
    component: ProductView
  },
  {
    path: &#39;/:category_slug&#39;,
    name: &#39;Category&#39;,
    component: CategoryView
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) =&gt; {
  if (to.matched.some(record =&gt; record.meta.requireLogin) &amp;&amp; !store.state.isAuthenticated) {
    next({ name: &#39;LogIn&#39;, query: { to: to.path } });
  } else {
    next()
  }
})

export default router

Also for some reason it create an infinite loop for component ProductBox use inside HomView.vue.
VUE: Error `Cannot use 'in' operator to search for 'path' in undefined`; infinite loop; did check similiar question on Stackoverflow. github,web below
You can see inside HomeView.vue the method getLatestProducts() hook mounted make an axios api call, updated latestProducts data and cause ProductBox to rerender.

I have tried different hook for Axios call beforeCreated() and created(). It doesn't work.
This is where I use the component
HomeView.vue

&lt;template&gt;
  &lt;div class=&quot;home&quot;&gt;
    &lt;section class=&quot;hero is-medium is-dark mb-6&quot;&gt;
        &lt;div class=&quot;hero-body has-text-centered&quot;&gt;
            &lt;p class=&quot;title mb-6&quot;&gt;
                Welcome to NoobStore
            &lt;/p&gt;
            &lt;p class=&quot;subtitle&quot;&gt;
                The most honest store online don&#39;t sell you anything
            &lt;/p&gt;
        &lt;/div&gt;
    &lt;/section&gt;

    &lt;div class=&quot;columns is-multiline&quot;&gt;
      &lt;div class=&quot;column is-12&quot;&gt;
          &lt;h2 class=&quot;is-size-2 has-text-centered&quot;&gt;Latest products&lt;/h2&gt;
      &lt;/div&gt;

      &lt;ProductBox 
        v-for=&quot;product in latestProducts&quot;
        v-bind:key=&quot;product.id&quot;
        v-bind:product=&quot;product&quot; /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import axios from &#39;axios&#39;

import ProductBox from &#39;@/components/ProductBox&#39;

export default {
  name: &#39;HomeView&#39;,
  data() {
    return {
      latestProducts: []
    }
  },
  components: {
    ProductBox
  },
  mounted() {
    this.getLatestProducts()

    document.title = &#39;Nhan`s Store | Vuejs demo&#39;
  },
  methods: {
    async getLatestProducts() {
      this.$store.commit(&#39;setIsLoading&#39;, true)
      try {
        const res = await axios.get(&#39;/api/v1/latest-products/&#39;)
        this.latestProducts = res.data
      } catch(error) {
      console.log(error)
      }

      this.$store.commit(&#39;setIsLoading&#39;, false)
    }
  }
}

答案1

得分: 1

检查您的 API 响应,与预期不符。您获得的是一个 HTML 页面,而不是 JSON。

英文:

check your api response, not expected. you got an html page not json

huangapple
  • 本文由 发表于 2023年6月27日 19:31:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定