【问题标题】:Uncaught (in promise) TypeError: Cannot use 'in' operator to search for 'validateStatus' inUncaught (in promise) TypeError: Cannot use 'in' operator to search 'validateStatus' in
【发布时间】:2021-02-21 14:27:27
【问题描述】:

我得到 ** Uncaught (in promise) TypeError: Cannot use 'in' operator to search 'validateStatus' in 5f8425a33a14f026f80133ed** 其中 5f8425a33a14f026f80133ed 是传递给 axios url 的 id

我想根据用户 ID 显示服务。我的网址在邮递员中完美运行,但是当我从 veux 商店访问它时,它会出错。

services.js(商店)

import axios from 'axios';

const state = {
    services : {},
    status: '',
    error: null
};

const getters = {
    services : state => { return state.services }
};

const actions = {
      async fetchServices({commit}, userId) {
        let res = await axios.get('http://localhost:5000/api/services/displayUser' , userId)
        commit('setProducts', res.data)
        return res;          
    }
};

const mutations = {
    setProducts (state, items) {
        state.services= items
    },
};

export default {
    state,
    actions,
    mutations,
    getters
};

这就是我调用动作的方式:

computed: {
      ...mapGetters(["services"]),
    }, 
  methods: {
    ...mapActions(["fetchServices"]),
    getData(){
      this.fetchServices(this.user._id)
    },
  },
    async created() {
      await this.getProfile();
      await this.getData();
    }

axios路由定义为

router.get('/displayUser', (req,res) => {
    const query = user =  req.body ;
    Services.find(query)
        .exec((err, services) => res.json(services))
})

错误截图:

Error screenshot

【问题讨论】:

    标签: mongodb vue.js axios vuex


    【解决方案1】:

    GET 请求不应有正文。要么使用查询参数,要么使用路径中的 id,要么使用 POST 请求。 如果是查询参数,这可能如下所示:

    let res = await axios.get('http://localhost:5000/api/services/displayUser' , { params: { userId })
    
    router.get('/displayUser', (req,res) => {
        const query = user =  req.query;
        Services.find(query)
            .exec((err, services) => res.json(services))
    })
    

    【讨论】:

      【解决方案2】:

      这对我也有用:

      前端:Vue Js

      let res = axios.get("http://localhost:3000/api/v1/role/getRoleByName", 
      { params: { roleName: "name of role you want to send as params" },
      });
      

      在后端:Node Js

      router.get('/getRoleByName', (req,res)=>{
        let roleName = req.query.roleName;
        roleModule.getRoleByName(roleName).then(data =>{
          response.json(res,data)
        }
          ).catch(err=> {
            response.badRequest(res, err);
          })
      });
      

      【讨论】:

        【解决方案3】:

        这是一个愚蠢的错误 axios.post req。

        async addTodo({ commit }, title) {
              try {
                const res = await axios.post(BASE_URL, { title, complete: false });
                commit("newTodo", res.data);
              } catch (err) {
                console.log(err.message);
              }
            },
        

        【讨论】:

          猜你喜欢
          • 2016-10-11
          • 2013-02-04
          • 1970-01-01
          • 2015-09-17
          • 1970-01-01
          • 2021-10-06
          • 2021-10-06
          • 1970-01-01
          • 2018-05-07
          相关资源
          最近更新 更多