deno - 如何使用 denodb 执行“where”查询?
问题描述
我正在尝试注册用户,但出现错误:
[uncaught application error]: TypeError - Cannot read properties of undefined (reading 'where')
这是代码:
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" };
}
这是我的模型:
// 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;
解决方案
不得不取消注释:
db.link([Users]);
推荐阅读
- amazon-web-services - 使用过时的市场 AMI 启动 AMI:如何选择或升级?
- java - 我已经使用用户输入指定了数组的大小,但我的 for 循环只输入 size-1 时间
- r - 如果所有观察值都是 0 或 1,如何自动将列转换为因子数据类型?
- node.js - 在nodejs express中上传多个文件,将目标名称作为新的对象ID
- javascript - Javascript 滚动顶部
- reactjs - 将自定义“全部”(总计)行添加到可像任何其他行一样选择的 ag 网格的顶部
- r - R闪亮的渲染表 - 如何更改列宽以包装特定列的文本?
- python - 是否可以从其他笔记本复制和粘贴 Jupyter Notebook 单元格并让新笔记本能够调用声明的变量?
- flutter - Mi android manifest.xml no reconoce los permisos
- kubernetes - 如何强制所有 kubernetes 服务(代理、kublet、apiserver...、容器)将日志写入 /var/logs