0

I am beginner in nodejs and reactjs, I develope an application from reactjs and nodejs, and i use postgresql for database , when i try to fetch employeedetails by calling api in server.js that time it shows datalogs in VScode console, but If i see in developer console in my browser then there is no data fetched from database, anyone help me to solve this problem?

server.js


    var restify=require('restify')
    const {empdetails,empdetails_id } = require('./Function');
    var server=restify.createServer() //server created
    
    server.get('/empdetails',empdetails)
    server.get('/empdetails_id/:id',empdetails_id)
    
    server.listen(8080, function(){
        console.log("server started...")
    })

function.js


    var Sequelize=require('sequelize')
    
    const connection = new Sequelize('mydb', 'postgres', '123456', {
        host: 'localhost',
        dialect:  'postgres' 
      });
    
      var Emp=connection.define('emp',{
        fullName: {
            type: Sequelize.STRING
          },
          email: {
            type: Sequelize.STRING
          }    
    })
    
    module.exports ={
    
       
    // employee details fetched
        empdetails: function empdetails(req,res,next){
            // res.send('employee details ')
            connection.sync().then(function(){
                Emp.findAll().then(function(emps){
                              console.log(emps)
                              var sample=emps
                              console.log(sample)
                              res.send(emps);
                  })
              })
        },
    // employee details by ID
        empdetails_id: function empdetails_id(req,res,next){
            res.send('employee details of :  '+req.params.id)
            connection.sync().then(function(){
                Emp.findByPk(req.params.id).then(function(emps){
                              console.log(emps)
                  })
              })
        },
        
     };

  • Just to mention, you are calling sequelize.sync each time on a request. Which will cause a database reset if there is a change on a model. Also there is no need it. It is just for creating the database and should be called once just before starting the nodejs server for "development" purposes. For production you don't use sequelize.sync. You should check sequelize migrations. https://sequelize.org/master/manual/migrations.html – Mansur Jul 30 '20 at 10:40

2 Answers2

0

I'm sorry, I don't use restify and sequelize. But to make a simple API call, you can use fetch:

fetch('urlToFetch').then(res => {
    if(res.ok) {
        console.log(res.json())
    } 
    else {
        console.log('Error')
    }
})
.catch(error => {
    console.log('Error: ' + error.message)
})

Documentation: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

0
var Sequelize=require('sequelize')

const connection = new Sequelize('mydb', 'postgres', '123456', {
    host: 'localhost',
    dialect:  'postgres' 
  });

  var Emp=connection.define('emp',{
    fullName: {
        type: Sequelize.STRING
      },
      email: {
        type: Sequelize.STRING
      }    
})

module.exports ={

   
// employee details fetched
    empdetails: function empdetails(req,res,next){
        // res.send('employee details ')
        connection.sync().then(function(){
            Emp.findAll().then(function(emps,error){
                           
                          //make sure you can return response
                      res.status(200).send({
                      data: emps,
                      message: "Success business list",
                       status: 0,
                });
              })
          })
    },
// employee details by ID
    empdetails_id: function empdetails_id(req,res,next){
        res.send('employee details of :  '+req.params.id)
        connection.sync().then(function(){
            Emp.findByPk(req.params.id).then(function(emps,error){

                          res.status(200).send({
                      data: emps,
                       message: "Success business list",
                    status: 0,
        });
              })
          })
    },
    
 };

Hello, Try this code here you need to return response with status.

I hope it will work for you. thank you

aum trivedi
  • 129
  • 4