Unable to access sent messages using JavaScript in Postman
P粉885035114
P粉885035114 2024-04-03 22:02:38
0
1
461

Using the code below, I can enter information into my MySQL table in the post method and the information is logged in the table. I also check it in js terminal. Using the "node api.js" command, the last information seems to be registered. But when I check with Postman, the last added information is not showing.

const mysql = require('mysql');
const express = require('express');
const app = express();
const port = 3000;

let cevap;

const baglanti = mysql.createConnection({
    host: 'example',
    user: 'example',
    password: 'example',
    database: 'example'

});

baglanti.connect();

baglanti.query('SELECT * FROM users', function (error, results, fields) {
    if (error) throw error;

    cevap = results;
    console.log(results);
    baglanti.end();
});

app.post('/users', (req, res) => {
    res.send(cevap);
});

app.listen(port, () => {
    console.log("worked"); 
});

If you look at the first photo, I can see the information added in the terminal, but the last entered information is not visible in the second photo (i.e. on the postman screen).

  1. Terminal picture
  2. Postman Picture

What I want is to be able to view all added users in Postman. When I add a new user, I immediately see it from postman (already registered to the MySQL table)

P粉885035114
P粉885035114

reply all(1)
P粉238433862

Every time you need new data, you have to query the database for the new data.

Ofc. You could cache it or something, but that's probably too advanced for this case.

const mysql = require('mysql');
const express = require('express');
const app = express();
const port = 3000;

const sql = mysql.createConnection({
    host: 'example',
    user: 'example',
    password: 'example',
    database: 'example',
});

sql.connect();

app.post('/users', (req, res, _next) => {
    sql.query('SELECT * FROM users', function (error, results, fields) {
        if (error) throw error;

        console.log(results);
        res.send(results);
    });
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`)
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template