Getting a "not a function" error when trying to create a Signin with JWT

Viewed 923

Disclaimer: this is my first project with react, mongo and js. I'm following a tutorial and trying to adapt it to my needs.

I'm trying to signin to a DB, get some values from a user and return them just so I know I managed to achieve a connection. I know that the credentials are being passed and are ok. When wrong credentials are passed instead, it works as intended and return an error.

I got reducers for 3 states: request, success and fail. In both success and fail the loading is set to false upon being changed to, but when I enter the correct credentials the loading remains on the screen. Meaning it probably got stuck during the request validation.

Login Screen with XHR window

When looking through the terminal, this is the error that I get on the log.

(node:13688) UnhandledPromiseRejectionWarning: TypeError: (0 , _util.default) is not a function at _callee$ (C:\Users\Andor\Documents\GitHub\NaturaVARK\backend\routes/userRoute.js:18:16) at tryCatch (C:\Users\Andor\Documents\GitHub\NaturaVARK\node_modules\regenerator-runtime\runtime.js:63:40) at Generator.invoke [as _invoke] (C:\Users\Andor\Documents\GitHub\NaturaVARK\node_modules\regenerator-runtime\runtime.js:293:22) at Generator.next (C:\Users\Andor\Documents\GitHub\NaturaVARK\node_modules\regenerator-runtime\runtime.js:118:21) at asyncGeneratorStep (C:\Users\Andor\Documents\GitHub\NaturaVARK\backend\routes\userRoute.js:16:103) at _next (C:\Users\Andor\Documents\GitHub\NaturaVARK\backend\routes\userRoute.js:18:194) at processTicksAndRejections (internal/process/task_queues.js:93:5)
at runNextTicks (internal/process/task_queues.js:62:3) at processImmediate (internal/timers.js:434:9)

I double checked both userRoute.js and util.js. Both are identical to the source code of the tutorial I'm following, went so far as to copy and paste them, just to be sure. The problems persists. The line that brings up the problem seem to be 18 and 16, those would be the token and email field at res.send. I selected the relevant parts of the code and put here, as well as the repository link in case I might've missed something.

userRoute.js

import express from 'express';
import User from '../models/userModel';
import getToken from "../util";

    const router = express.Router();
    
    router.post('/signin', async (req, res) => {
        const signinUser = await User.findOne({
          email: req.body.email,
          password: req.body.password,
        });
        if (signinUser) {
          res.send({
            _id: signinUser.id,
            name: signinUser.name,
            email: signinUser.email,
            isAdmin: signinUser.isAdmin,
            token: getToken(signinUser),
          });
        } else {
          res.status(401).send({ message: 'Invalid Email or Password.' });
        }
      });

export default router;

util.js

import jwt from 'jsonwebtoken';
import config from './config';
const getToken = (user) => {
  return jwt.sign(
    {
      _id: user._id,
      name: user.name,
      email: user.email,
      isAdmin: user.isAdmin,
    },
    config.JWT_SECRET,
    {
      expiresIn: '48h',
    }
  );
};

export { getToken };

Github Repository

Any insight is more than welcome.

1 Answers

Since the data exported was an object {getToken}, you have to import as an object. So getToken becomes {getToken}, as it was not exported as a default value. For more insight check this out https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

Try this:

import express from 'express';
import User from '../models/userModel';
import {getToken} from "../util";

    const router = express.Router();
    
    router.post('/signin', async (req, res) => {
        const signinUser = await User.findOne({
          email: req.body.email,
          password: req.body.password,
        });
        if (signinUser) {
          res.send({
            _id: signinUser.id,
            name: signinUser.name,
            email: signinUser.email,
            isAdmin: signinUser.isAdmin,
            token: getToken(signinUser),
          });
        } else {
          res.status(401).send({ message: 'Invalid Email or Password.' });
        }
      });

export default router;
Related