I'm new to deno, and I'm trying to create a post request with oak following this tutorial by freecodecamp (https://www.youtube.com/watch?v=TQUy8ENesGY). And I realized that many things changed and it took me hours to find those many things using oak.
But now I'm stuck in using the User model to create a new user (at the 2 hours and 33 mins in the video). It gives me this error:-
Type 'User' is missing the following properties from type 'Promise': then, catch, [Symbol.toStringTag]
Here's my code:-
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;
router.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;
I want to know the best practice of using the model, and whether if I have to use the Interface type or not.
And also, if there's a better way to write the post request, please do tell me.
Thanks...
It should be
let user = await User.findOne({email});