0
const form = document.getElementById('vote-form')
form.addEventListener('submit',e => {
  let choice = document.querySelector('input[name=os]:checked').value;
  let data = {os: choice};

  fetch("http://localhost:3000/poll",{ // *** getting error here
    method:'post',
    body:JSON.stringify(data),
    headers: new Headers({
      'Content-Type':'application/json'
    })
  }).then(res => res.json()).then((data) => {console.log(data)}).catch(err => console.log(err))    
e.preventDefault();
});

Here`s the backend code: Its my main router file, where all related routes are placed

const express = require('express')
const router = express.Router()
const Pusher = require('pusher')

let pusher=new Pusher({
  appId:'*******',
  key:'*****',
  secret:'******',
  cluster:'*****',
  encrypted:true
});

router.get('/',(req,res) => { //it actually means /poll (as mentioned in app.js)
  res.send('polling starts');
})

router.post('/',(req,res)=>{
pusher.trigger('os-poll','os-vote',{
  points: 1,
  os: req.body.os
});
res.json({success:true,message:"thanks for voting"})
});

module.exports=router

I am getting Internal Server Error - 500 Status code. Why so?? What could be the possible reason?

  • Please show your back-end code. – Kevin Mar 05 '18 at 23:22
  • @Kevin added the same. –  Mar 05 '18 at 23:25
  • I saw your code has a character "1" before module.exports. – Kevin Mar 05 '18 at 23:30
  • @Kevin I am facing issues in that promise code. Fetch is not returning anything I guess. Or it is not working as it is supposed to. –  Mar 05 '18 at 23:33
  • If you get 500 error, it should be your back-end code has some problem. I did not use pusher library before. I suggest you remove the pusher.trigger from your back-end code and just simply send back some text to see whether it works. If it does, you should take a look the document of pusher and make it right. – Kevin Mar 05 '18 at 23:37
  • Let me try. That's a great idea. –  Mar 05 '18 at 23:39
  • Try adding breakpoints to your app.get / app.post and see if they're even triggered. – Jake T. Mar 05 '18 at 23:41
  • @Kevin yes it worked but when I included that trigger method again, I get an error `SyntaxError: Unexpected token < in JSON at position 0` –  Mar 05 '18 at 23:42
  • @JakeT. the main issue is in pusher.trigger method, I am unable to figure out this one `SyntaxError: Unexpected token < in JSON at position 0` –  Mar 05 '18 at 23:43
  • Sorry. I do not know the pusher library. Please reorganize your post and address the main issue. So, somebody who familiars with pusher will answer it. – Kevin Mar 05 '18 at 23:47
  • @Kevin thanks for the effort buddy –  Mar 05 '18 at 23:49
  • If you updated your code, show the update. The symbol you've posted shouldn't be there in the code you've shown. – Jake T. Mar 05 '18 at 23:55
  • I think the options you're passing aren't valid. Try changing your `{points:1, os: req.body.os}` to `{message:"test message"}` – Jake T. Mar 06 '18 at 00:01
  • And if that's the case, you've gotta find the proper syntax to pass the attributes you want. Likely data? `{message:"test message", data: { points:1, os: req.body.os}}` – Jake T. Mar 06 '18 at 00:02
  • we need to send JSON data so, I am bound to stringify, I cannot send an object as suggested. –  Mar 06 '18 at 06:11

1 Answers1

0

I just had to use body-parser package for the same.