Fahad Subzwari
Fahad Subzwari

Reputation: 2325

Facing issue in making rest api(Get) in nodejs

I have a table named "interviewCategory" and in that there are categories.Now i want to fetch all categories through Rest api. in server.js file i have included my AdminUtils file in which there is function to send responses to getInterviewCategories. Here it is in my server.js file

enter image description here

here is my adminUtils.js file code

var connection = require('./../config');
var helpers = require('../helpers/helper');

module.exports.getInterviewCategories =  function(req,res) {
console.log('in getInterviewCategories function')
helpers.getAllFromDb()
.then(function(categories){
    if(categories == null){
        res.status(400).send({
            meta : {status : 400, message : 'There is some error with query'}
        });
    }
    else{
        res.status(200).send({
            meta : {status : 200 , message : 'Success'},
            data : categories
        });
    }
})
.catch(function(err){
    res.status(500).send({
        meta : {status : 500, message : 'Internal Server Error'}
    });
  })
 }

Here is my helpers.js file in which there is a function "getAllFromDb" to fetch all categories from db

exports.getAllFromDb = function getAllFromDb() {
console.log('in getAllFromDb Function');
var query = "SELECT * from interviewcategory";
return new Promise(function(resolve,reject){
    connection.query(query,(err,result)=>{
        if(err){
            return reject(err);
        }
        else{
            return resolve(result);
        }
    })
  })
 }

Here is my api url

enter image description here

Here is the error which i am getting when i am hitting the api using POSTMAN.

enter image description here

Here is my response on console

enter image description here

Upvotes: 0

Views: 29

Answers (1)

user-developer
user-developer

Reputation: 586

Your express route is wrong. You should create the GET route. So, change the following

app.post('/api/admin/interview/getCategories', AdminUtills.getInterviewCategories)

to

app.get('/api/admin/interview/getCategories', AdminUtills.getInterviewCategories)

Upvotes: 1

Related Questions