How to perform a "where" query using denodb?

295 Views Asked by At

I'm trying to register a user and I get an error:

[uncaught application error]: TypeError - Cannot read properties of undefined (reading 'where')

Here is the code:

  async register(context: any) {
    const body = JSON.parse(await context.request.body().value);
    const existing = await Users.where("email", body.email).get();

    if (existing.length) {
      context.response.status = 400;
      return (context.response.body = { message: "User already exists" }); 
    }   

    const hashedPassword = await Users.hashPassword(body.password);
    const user = await Users.create({
      email: body.email,
      hashedPassword,
    }); 

    context.response.body = { message: "User created" };
  }

Here is my model:

// import { Model, DataTypes } from "https://deno.land/x/denodb/mod.ts";
import { DataTypes, Model } from "https://deno.land/x/denodb/mod.ts";
import * as bcrypt from "https://deno.land/x/bcrypt/mod.ts";
import {
  create,
  getNumericDate,
  verify,
} from "https://deno.land/x/djwt/mod.ts";
import { JwtConfig } from "../middleware/jwt.ts";
import { db } from "../db.ts";

class Users extends Model {
  static table = "users";
  static timestamps = true;

  static fields = {
    id: {
      primaryKey: true,
      type: DataTypes.STRING,
    },
    email: {
      type: DataTypes.STRING,
      unique: true,
    },
    hashedPassword: {
      type: DataTypes.STRING,
    },
  };

  static defaults = {
    id: crypto.randomUUID(),
  };

  // ...
  static async hashPassword(password: string) {
    const salt = await bcrypt.genSalt(8);
    return bcrypt.hash(password, salt);
  }

  static generateJwt(id: string) {
    // Create the payload with the expiration date (token have an expiry date) and the id of current user (you can add that you want)
    const payload = {
      id,
      iat: getNumericDate(new Date()),
    };
    // return the generated token
    return create({ alg: "HS512", typ: "JWT" }, payload, JwtConfig.secretKey);
  }
}

//db.link([Users]);
//await db.sync();

export default Users;


1

There are 1 best solutions below

0
chovy On

Had to uncomment this:

db.link([Users]);