7

I am running server in nodejs, while executing the code of server i am getting an error as "(node:7692) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Request failed with status code 400 (node:7692) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code".

this is my serverrender.js code

import axios from 'axios';
import config from './config';

axios.get('${config.serverUrl/api/contests')
.then(resp=>{
    console.log(resp.data);
});

this is my srver.js code

    import config from './config';
    import apiRouter from './api';
    import express from 'express';
    import path from 'path';
    import sassMiddleware from 'node-sass-middleware';

    import './serverRender';
    const server=express();
    server.set('view engine','ejs');

    server.use(sassMiddleware({
        src:path.join(__dirname,'sass'),
        dest:path.join(__dirname,'public')
    }));

    server.get('/',(req,res)=>{

        res.render('index',{
            content:"... "
        });


});
server.use(express.static('public'));

server.use('/api',apiRouter);

server.listen(config.port, config.host, () =>{
    console.info('express listening on port ',config.port);
});
Abhilash Muttalli
  • 1,279
  • 6
  • 19
  • 23
  • Possible duplicate of [What is Unhandled Promise Rejection](http://stackoverflow.com/questions/40500490/what-is-unhandled-promise-rejection) – Or B Jan 18 '17 at 07:10
  • 1
    The `Unhandled Promise Rejection` is because you don't have the `catch` callback. As for the other, check the url; it's `${config.serverUrl/api/contests`. You have `${` and the bracket does not close. You sure that is correct? – Kousha Jan 18 '17 at 07:18

2 Answers2

5

You should add a catch to the get call:

import axios from 'axios';
import config from './config';

axios.get('${config.serverUrl/api/contests')
  .then(resp=>{
    console.log(resp.data);
  })  
  .catch(error=>{

  });
Gilbert Nwaiwu
  • 687
  • 2
  • 13
  • 37
1

You need to enclose the opening parentheses of the config variable.

import axios from 'axios';
import config from './config';

axios.get(`${config.serverUrl}/api/contests`)
  .then(resp=>{
    console.log(resp.data);
  })  
  .catch(error=>{

  });
Conrad747
  • 45
  • 7