Home > Net >  How do I use a Get query?
How do I use a Get query?

Time:07-07

Hello I created a form allowing me to choose which database table I want to observe. I then want to make a query based on the selected data but it seems that the format or my way of doing it does not seem good.

Here is my fetch function:

temp_select: 'temperature'

async exportData(){
const data_select = encodeURIComponent(this.temp_select);
const url = `http://192.168.1.51:3000/api/v1/export/${data_select}`;
    fetch(url)
        .then(res => res.text())
        .then((result) => {
            console.log(typeof(data_select));
            const data = JSON.parse(result);
            console.log(data);
        })
        .catch((error) => {
            console.log(error)
        });

},

and here is my query function

async function exportDatabase(req, res){
const data_selected = req.params.data_select;
return db.any('SELECT $1 FROM tag_7z8eq73', [data_selected])
    .then(rows => {
        res.json(rows)
    })
    .catch(error => {
        console.log(error)
    }); 

}

the database is loaded but here is what I observe

enter image description here

It works correctly in this form:

async function exportDatabase(req, res){
return db.any('SELECT temperature FROM tag_7z8eq73')
    .then(rows => {
        res.json(rows)
    })
    .catch(error => {
        console.log(error)
    }); }

enter image description here

I'm working with node.js and vue.js

Can someone enlighten me?

edit : @Quentin Thank you for your answer you help me understand how the GET request works. Here is my problem solved :

fetch function :

async exportData(){
const data_select = encodeURIComponent(this.temp_select);
const url = `http://192.168.1.51:3000/api/v1/export/${data_select}`;
    fetch(url)
        .then(res => res.text())
        .then((result) => {
            console.log(typeof(data_select));
            const data = JSON.parse(result);
            console.log(data);
        })
        .catch((error) => {
            console.log(error)
        });

},

Query function:

async function exportDatabase(req, res){
const data_selected = req.params.data_select;
return db.any('SELECT '   data_selected   ' FROM tag_7z8eq73')
    .then(rows => {
        res.json(rows)
    })
    .catch(error => {
        console.log(error)
    }); }

my route :

router.get('/export/:data_select', db.exportDatabase);

CodePudding user response:

method: 'GET',

You are making a GET request


headers: {
    'Content-Type': 'application/json',
},

This claims that the request body is JSON.

GET requests cannot have a request body, so this cannot be true.

(Technically they can, but really shouldn't, but fetch won't let you give a GET request a body).

Remove this header.


params: JSON.stringify({
    data_select: this.temp_select,          
}),

fetch does not have a params option, so this is nonsense. Remove it.


async function exportDatabase(req, res){
     return db.any('SELECT $1 FROM tag_7z8eq73', [req.params.data_select])

Assuming you are using Express, look at the definition of params:

This property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to {}.

Which means you need to define data_select when you define the route:

app.get('/api/v1/export/:data_select', exportData);

And then put the value in the URL when you make the request:

const data_select = encodeURIComponent(this.temp_select);
const url = `http://192.168.1.51:3000/api/v1/export/${data_select}`;
fetch(url).then(...);
  • Related