我是deno的新手,我正试图按照freecodecamp的本教程用oak创建一个post请求(https://www.youtube.com/watch?v=TQUy8ENesGY)。我意识到很多东西都变了,我花了几个小时才用橡木找到这些东西。
但现在我陷入了使用用户模型创建新用户的困境(在视频中的2小时33分钟(。它给了我这个错误:-
类型"User"在类型中缺少以下属性"Promise":然后,catch,[Symbol.toStringTag]
这是我的代码:-
authcontroller.ts:-
import { RouterContext, hash, compare } from "../deps.ts";
import User from "../models/user.model.ts";
class AuthController {
async register(ctx: RouterContext){
const result = ctx.request.body({ type: "form-data" });
const data = (await result.value.read()).fields;
const name = data['name'];
const password = data['password'];
const email = data['email'];
let user = User.findOne({email});
if (user){
ctx.response.body = { message: "Email is already used!" };
ctx.response.status = 409;
}
const hashedPassword = hash(password);
// user = new User({name, email});
ctx.response.status = 201;
console.log(data);
}
login(ctx: RouterContext){
ctx.response.body = "This is new login"
}
}
const authController = new AuthController();
export default authController;
user.model.ts:-
import usersCollection from "../mongo.ts";
class User {
public id: string;
public name: string;
public email: string;
public password: string;
constructor({ id='', name='', email='', password=''}){
this.id = id;
this.name = name;
this.email = email;
this.password = password;
}
static findOne(params: object){
return usersCollection.findOne(params);
}
}
export default User;
路由器.ts:-
import { Router, RouterContext } from "./deps.ts";
import authController from "./controllers/authController.ts";
const router = new Router();
router.get("/", (ctx) => {
ctx.response.body = "New Hello world!"
});
router.post("/api/login", authController.login);
router.post("/api/register", authController.register);
export default router;
我想知道使用该模型的最佳实践,以及是否必须使用接口类型。
此外,如果有更好的方式来写帖子请求,请告诉我。
谢谢。。。
应该是let user = await User.findOne({email});