【发布时间】:2021-02-26 21:20:59
【问题描述】:
我正在向 axios get 请求传递一个参数。它适用于邮递员,但不适用于我的代码。我不知道我在哪里犯了错误。
我只想要一个来自 db 的特定数据,但我正在接收集合中可用的所有数据。但是通过邮递员,我得到了所需的数据
后端路由:
router.get('/displayUser', (req,res) => {
const query = user = req.body ;
Services.find(query)
.exec((err, services) => res.json(services))
})
axios 调用:我尝试了两种不同的方法,但都不起作用
方法一:
getData: async function () {
const user = this.userId
console.log(user)
let res = await axios.get('http://localhost:5000/api/services/displayUser' , { params: { user }})
console.log(res.data);
}
方法二:
getData: async function () {
var data = JSON.stringify({"user":this.userId});
console.log(data)
var config = {
method: 'get',
url: 'http://localhost:5000/api/services/displayUser',
headers: {
'Content-Type': 'application/json'
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
}
当我在控制台中获取数据时,我得到了集合中可用的所有 3 个对象,而不是与用户 ID 相关的特定对象
但在邮递员中它可以按需要工作
【问题讨论】:
-
尝试将数据发送到 axios 而不对其进行字符串化(即) var data = {"user":this.userId};
-
我试过了,还是不行@AmaarshallYaswankar
-
Ayyo,这让我很困惑 :( @Jayashree
-
我也一直为此头疼:'(@AmaarshallYaswankar
-
很少见的场景哈哈
标签: mongodb vue.js get axios postman