Home > database >  TypeError: signin.handleSignin is not a function
TypeError: signin.handleSignin is not a function

Time:12-06

I"ve seen this question asked before, but I cannot quite figure out what it is I'm doing wrong. I have an endpoint: app.post('/signin', signin.handleSignin(db, bcrypt)) in my server.js file. The console is alerting me that signin.handleSignIn is not a function. This endpoint is linked to a file in my controllers folder that is signin.js that looks like this:

const handleSignin = (db, bcrypt) => (req, res) => {
    const { email, password } = req.body;
    if (!email || !password) {
      return res.status(400).json('incorrect form submission');
    }
    db.select('email', 'hash').from('login')
      .where('email', '=', email)
      .then(data => {
        const isValid = bcrypt.compareSync(password, data[0].hash);
        if (isValid) {
          return db.select('*').from('users')
            .where('email', '=', email)
            .then(user => {
              res.json(user[0])
            })
            .catch(err => res.status(400).json('unable to get user'))
        } else {
          res.status(400).json('wrong credentials')
        }
      })
      .catch(err => res.status(400).json('wrong credentials'))
  }`enter code here`
  
 export default handleSignin;

server.js code:

import express, { application, response } from "express";
import bcrypt, { hash } from "bcrypt-nodejs";
import cors from "cors";
import knex from "knex";
import signin from "./controllers/signin.js"
const db = knex({
  client: "pg",
  connection: {
    host: "127.0.0.1",
    port: 5432,
    user: "postgres",
    password: "test",
    database: "smart-brain",
  },
});

db.select("*")
  .from("users")
  .then((data) => {});

const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cors());


app.get("/", (req, resp) => {
  resp.send(db.users);
});

app.post('/signin', signin.handleSignin(db, bcrypt))

app.post("/signin", (req, resp) => {
  db.select("email", "hash")
    .from("login")
    .where("email", "=", req.body.email)
    .then((data) => {
      const isValid = bcrypt.compareSync(req.body.password, data[0].hash);
      if (isValid) {
        return db
          .select("*")
          .from("users")
          .where("email", "=", req.body.email)
          .then((user) => {
            console.log(user);
            resp.json(user[0]);
          })
          .catch((err) => resp.status(400).json("Unable to get user"));
      } else {
        resp.status(400).json("Wrong credentials");
      }
    })
    .catch((err) => resp.status(400).json("Wrong credentials"));
});

app.post("/register", (req, resp) => {
  const { name, email, password } = req.body;
  const hash = bcrypt.hashSync(password);
  db.transaction((trx) => {
    trx
      .insert({
        hash: hash,
        email: email,
      })
      .into("login")
      .returning("email")
      .then((loginemail) => {
        trx("users")
          .returning("*")
          .insert({
            name: name,
            email: loginemail[0],
            joined: new Date(),
          })
          .then((user) => {
            resp.json(user[0]);
          });
      })
      .then(trx.commit)
      .catch(trx.rollback);
  }).catch((err) => resp.status(400).json("unable to register"));
  // bcrypt.hash(password, null, null, function (err, hash) {
  //   console.log(hash);
  // });
});

app.get("/profile/:id", (req, resp) => {
  const { id } = req.params;
  db.select("*")
    .from("users")
    .where({ id })
    .then((user) => {
      if (user.length) {
        resp.json(user[0]);
      } else {
        resp.status(400).json("Not found");
      }
    })
    .catch((err) => resp.status(400).json("Error getting user"));
  // if (!found) {
  //   resp.status(400).json("not found");
  // }
});

app.put("/image", (req, resp) => {
  const { id } = req.body;
  db("users")
    .where("id", "=", id)
    .increment("entries", 1)
    .returning("entries")
    .then((entries) => {
      resp.json(entries[0]);
    })
    .catch((err) => resp.status(400).json("Unable to get entries"));
});

app.listen(3001, () => {
  console.log("App is running at port 3001");
});

CodePudding user response:

You didn't exported signin from signin.js, you exported handleSignin. so just import handleSignin and call it directly.

import handleSignin from "./controllers/signin.js"
...
app.post('/signin', handleSignin(db, bcrypt))
  • Related