0

I am following this tutorial here: https://www.youtube.com/watch?v=a-JKj7m2LIo

I got stuck around the 14 minute mark getting the following error message in the terminal:

(node:6248) UnhandledPromiseRejectionWarning: RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined
[0]     at ServerResponse.writeHead (_http_server.js:237:11)
[0]     at ServerResponse._implicitHeader (_http_server.js:228:8)
[0]     at write_ (_http_outgoing.js:616:9)
[0]     at ServerResponse.end (_http_outgoing.js:733:5)
[0]     at ServerResponse.send (D:\htdocs\mern\react-slack-clone\node_modules\express\lib\response.js:221:10)
[0]     at ServerResponse.sendStatus (D:\htdocs\mern\react-slack-clone\node_modules\express\lib\response.js:359:15)
[0]     at D:\htdocs\mern\react-slack-clone\server.js:31:13
[0]     at processTicksAndRejections (internal/process/task_queues.js:93:5)
[0] (node:6248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise
which was not handled with .catch(). (rejection id: 1)
[0] (node:6248) [DEP0018] 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

Still learning how to use React and Node, but I think the error is occurring in my server.js file.

My server.js code looks like this:

const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const Chatkit = require('pusher-chatkit-server')

const app = express()

const chatkit = new Chatkit.default({
instanceLocator: 'fhdsakfjsdalkfjdsalfjsdlajflsad',
key: 
'ruewioqruewfhdsakljfdsaljfsdlakjds483294'
})

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cors())

app.post('/users', (req, res) => {
  const { username } = req.body

  chatkit
    .createUser({
      name: username,
      id: username
    })
    .then(() => res.sendStatus(201))
    .catch(error => {
      console.log('Error:', error) // updated 
      /*if(error.error_type === 'services/chatkit/user_already_exists') {
        res.sendStatus(200)
      } else {
        res.sendStatus(error.statusCode).json(error)
      }*/
    })
})

const PORT = 3001
app.listen(PORT, err => {
  if (err) {
    console.error(err)
  } else {
    console.log(`Running on port ${PORT}`)
  }
})

Basically, when I submit a name, in the console's network tab, I should see the post, but I am not. The error message is all I get but I'm not sure how to decipher it.

Edit

Added console.log('Error:', error) to the code above and received the following error message in the terminal:

Error: ErrorResponse {
[0]   status: 404,
[0]   headers: {
[0]     'access-control-expose-headers': 'X-Envoy-Upstream-Service-Time, 
Server, Access-Control-Expose-Headers, Access-Control-Max-Age, Date',
[0]     'access-control-max-age': '86400',
[0]     'content-type': 'application/json',
[0]     date: 'Tue, 17 Mar 2020 18:07:17 GMT',
[0]     server: 'istio-envoy',
[0]     'x-envoy-upstream-service-time': '10',
[0]     'content-length': '209',
[0]     connection: 'close'
[0]   },
[0]   error: 'not_found',
[0]   error_description: 'Not found',
[0]   error_uri: 'https://docs.pusher.com/errors/not_found'
[0] }

I do not understand what this error message means. I check the URL provided by error_uri, and the message on that page read, "The server couldn't find the requested resource. Please make sure that the name of the resource you are requesting is correct." I am not entirely sure what that means.

Edit 2

I made the following change to app.post in an attempt to produce another terminal error, as follows:

app.post('/users', (req, res) => {
  const { username } = req.body

  chatkit
    .createUser({
      name: username,
      id: username
    })
    .then(() => res.sendStatus(201))
    .catch(error => {
      //console.log('Error:', error)
      if(error.error_type === 'services/chatkit/user_already_exists') {
        res.sendStatus(200)
      } else {
        //res.sendStatus(error.statusCode).json(error)
        res.sendStatus(500)
        console.log(JSON.stringify(error))
      }
    })
})

I received the following response in the terminal:

{"status":404,"headers":{"access-control-expose-headers":"Date, 
X-Envoy-Upstream-Service-Time, Server, Access-Control-Expose-Headers, 
Access-Control-Max-Age","access-control-max-age":"86400",
"content-type":"application/json","date":"Thu, 09 Apr 2020 02:34:22
 GMT","server":"istio-envoy",
 "x-envoy-upstream-service-time":"11",
"content-length":"209","connection":"close"},
"error":"not_found","error_description":
"Not found","error_uri":"https://docs.pusher.com/errors/not_found"}

I do not know what any of this means.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89
  • `res.sendStatus` fails with the error of "Invalid status code: undefined". Looking at your code, the only place where the status code is variable is in `res.sendStatus(error.statusCode).json(error)`. So `error.statusCode` is undefined. Is it `statusCode` or is it `status_code` perhaps ? Chatkit's API docs will tell. – cbr Mar 17 '20 at 17:44
  • You might want to console.log the error in the `catch` so you can see if the error has a status code property. – cbr Mar 17 '20 at 17:46
  • In the catch, I removed the if/else and used "console.log(error)", but nothing returned in the console. – John Beasley Mar 17 '20 at 18:00
  • I just noticed that under network, the status reads 'pending' and has been for quite some time. Thoughts? – John Beasley Mar 17 '20 at 18:01
  • Yup, the frontend never gets a response from your server because it crashes, that's normal. Might want to add an express [error handler](https://expressjs.com/en/guide/error-handling.html). – cbr Mar 17 '20 at 18:03
  • Hmm, nothing in the console? Try logging `console.log('Error:', error)` - see if the "Error:" text appears. Oh, and make sure you're looking at the node server's terminal, not the frontend's :) – cbr Mar 17 '20 at 18:05
  • Ok, I am updating my question using your suggestion. – John Beasley Mar 17 '20 at 18:09

0 Answers0