Mongodb 带参数的Axios get请求不工作
我正在向axios get请求传递一个参数。它可以在postman上正常工作,但不能与我的代码一起工作。我不知道我在哪里犯了错误 我只想从数据库中获取一个特定数据,但我正在接收集合中可用的所有数据。但是有了邮递员,我得到了想要的数据 后端路由:Mongodb 带参数的Axios get请求不工作,mongodb,vue.js,get,axios,postman,Mongodb,Vue.js,Get,Axios,Postman,我正在向axios get请求传递一个参数。它可以在postman上正常工作,但不能与我的代码一起工作。我不知道我在哪里犯了错误 我只想从数据库中获取一个特定数据,但我正在接收集合中可用的所有数据。但是有了邮递员,我得到了想要的数据 后端路由: router.get('/displayUser', (req,res) => { const query = user = req.body ; Services.find(query) .exec((err, servic
router.get('/displayUser', (req,res) => {
const query = user = req.body ;
Services.find(query)
.exec((err, services) => res.json(services))
})
axios call:我尝试了两种不同的方法,但都不起作用
方法1:
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);
}
方法2:
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相关的特定对象
但在《邮递员》中,它却能如愿以偿
我是这样做的:
- 当我需要帮助时:
- 当我需要发布时(req.body是json):
尝试将数据发送到axios而不进行字符串化(即)var data={“user”:this.userId};我试过这么做,但仍然不起作用@AmaarshallYaswankarAyyo,这让我很困惑:(@JayashreeI也用这个打断了我的头:“(@Amaarshallyaswankaryyo有点罕见的场景lol
app.get('/detail/:id', function (req, res) {
//console.log(req.params.id);
var url=urlDetail + "/" + req.params.id;
axios.get(url)
.then(function (response) {
// result=response.data;
res.render('database', { title: 'Detail' , dbs: response.data ,Version:pjson.version});
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
//console.log("ici always");
});
});
app.post('/carto/demande', function (req, res) {
let data;
console.log(req.params);
console.log(req.body);
var url=urlCartoDemande;
axios.post(url,req.body)
.then(function (response) {
data=response.data;
res.render('carto', { title : 'Demande' ,Version:pjson.version,mode:"resultat",data:data } );
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
});