search

Home  >  Q&A  >  body text

Unable to access sent messages using JavaScript in Postman

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粉885035114P粉885035114228 days ago401

reply all(1)I'll reply

  • P粉238433862

    P粉2384338622024-04-04 18:49:32

    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}`)
    });

    reply
    0
  • Cancelreply