0

I want to send a post to get data from mysql and display it to my thing, but it is not sending any post requests.

This is what my file looks like:

this is the place were I am sending the post request

podItems.forEach(item => {
    item.addEventListener('click', () => {
        i
        alert("testing alert");
        //podNum = item.id;
        var test ="dog";
        //email: userMail , rows: gridNum, pod:podNum
        $.post('http://localhost:3000/farm',
        { testsend: test},
        function(res){
            alert(res.testchange);
        });
    });
});

This is were i wanted to send it to to modify: pages.js

const express = require('express');
const authController = require('../controllers/auth');
const router = express.Router();

router.get("/", (req, res) => {
    res.render("index.hbs");
});

router.get("/produce", (req, res) => {
    res.render("produce.hbs");
});

router.get("/contact", (req,res) => {
    res.render("contact.hbs");
});

router.get("/signup", (req, res) => {
    res.render("signup.hbs");
});

router.get("/login", (req, res) => {
    res.render("login.hbs");
});

router.get("/forgot", (req, res) => {
    res.render("forget.hbs");
});

router.get("/change", (req, res) => {
    res.render("change.hbs");
});

router.get("/farm", authController.isLoggedIn, (req, res) => {
  //console.log(req.user);
    if( req.user ) {
      res.render('farm.hbs', {
        user: req.user
      });
    } else {
      res.redirect('/login');
    }
});

router.post("/farm"), async(req, res) => {
  console.log(req.body)
  
var gottest = "gotten";
res.send({
  testchange : gottest

});
}

router.get("/export", (req, res) => {
  res.render("export_page.hbs");
});


router.get('/profile', authController.isLoggedIn, (req, res) => {
    //console.log(req.user);
    if( req.user ) {
      res.render('profile.hbs', {
        user: req.user
      });
    } else {
      res.redirect('/login');
    }
    
  });

module.exports = router;

Some files that may relate: app.js

const express = require("express");
const path = require('path');
const mysql = require("mysql");
const dotenv = require('dotenv');
const cookieParser = require('cookie-parser');

dotenv.config({ path: './.env'});

const app = express();

const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'nodejs-login'
});

const plantdb = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'plantdb'
})


const publicDirectory = path.join(__dirname, './public')
app.use(express.static(publicDirectory));

app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cookieParser());

app.set('view engine', 'hbs');

db.connect( (error) =>{
    if(error) {
        console.log(error)
    }
    else{
        console.log("MySQL is in")
    }
})

plantdb.connect( (error) =>{
    if(error) {
        console.log(error)
    }
    else{
        console.log("MySQL is in")
    }
})

app.use('/',require('./routes/pages'));

app.use('/auth', require('./routes/auth'));

app.listen(3001, () => {
    console.log("Server started on Port 3001");
})

My main goal with this is to have the "user" enter a few buttons and then the page will display info from a MySQL server onto the page for them to view/edit. I am just stuck rn because the post is not sending anything. I have placed a few console logs to see if it even reaches the post but it only reaches up to the alert that says "testing alert". Why aren't doing the post request? or Is there a better way to send and receive data from MySQL for my program?

sachin
  • 1,075
  • 1
  • 7
  • 11
harry vu
  • 1
  • 2

0 Answers0