Jay*_*e K 1 mongodb vue.js axios vuex
我得到 ** Uncaught (in promise) TypeError: Cannot use 'in' operator to search for 'validateStatus' in 5f8425a33a14f026f80133ed** 其中 5f8425a33a14f026f80133ed 是传递给 axios url 的 id
我想根据用户 ID 显示服务。我的 url 在邮递员中工作得很好,但是当我从 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
};
Run Code Online (Sandbox Code Playgroud)
这就是我调用该操作的方式:
computed: {
...mapGetters(["services"]),
},
methods: {
...mapActions(["fetchServices"]),
getData(){
this.fetchServices(this.user._id)
},
},
async created() {
await this.getProfile();
await this.getData();
}
Run Code Online (Sandbox Code Playgroud)
axios 路由定义为
router.get('/displayUser', (req,res) => {
const query = user = req.body ;
Services.find(query)
.exec((err, services) => res.json(services))
})
Run Code Online (Sandbox Code Playgroud)
错误截图:
GET 请求不应有正文。要么使用查询参数,在路径中指明一个 id,要么使用 POST 请求。在查询参数的情况下,这可能如下所示:
let res = await axios.get('http://localhost:5000/api/services/displayUser' , { params: { userId })
Run Code Online (Sandbox Code Playgroud)
router.get('/displayUser', (req,res) => {
const query = user = req.query;
Services.find(query)
.exec((err, services) => res.json(services))
})
Run Code Online (Sandbox Code Playgroud)