How to perform a "where" query using denodb?

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 "";
import { DataTypes, Model } from "";
import * as bcrypt from "";
import { create, getNumericDate, verify,
} from "";
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 Answer

Had to uncomment this:

db.link([Users]);

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like