post 请求在 deno 中使用 oak 和 mongodb
post request using oak in deno with mongodb
我是 deno 的新手,我正在尝试按照 freecodecamp (https://www.youtube.com/watch?v=TQUy8ENesGY) 的本教程使用 oak 创建一个 post 请求。我意识到很多事情都变了,我花了几个小时才用橡木找到这么多东西。
但现在我一直在使用用户模型创建新用户(在视频的 2 小时 33 分钟处)。它给了我这个错误:-
Type 'User' is missing the following properties from type
'Promise': then, 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;
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;
我想知道使用模型的最佳实践,以及是否必须使用接口类型。
此外,如果有更好的方法来编写 post 请求,请告诉我。
谢谢...
应该是let user = await User.findOne({email});
我是 deno 的新手,我正在尝试按照 freecodecamp (https://www.youtube.com/watch?v=TQUy8ENesGY) 的本教程使用 oak 创建一个 post 请求。我意识到很多事情都变了,我花了几个小时才用橡木找到这么多东西。
但现在我一直在使用用户模型创建新用户(在视频的 2 小时 33 分钟处)。它给了我这个错误:-
Type 'User' is missing the following properties from type 'Promise': then, 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;
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;
我想知道使用模型的最佳实践,以及是否必须使用接口类型。
此外,如果有更好的方法来编写 post 请求,请告诉我。
谢谢...
应该是let user = await User.findOne({email});