我是 koa 和 postgresql 节点的新手。我创建了一个用户登录 api,但收到 404 未找到错误。当我在控制台上检查时,我的查询和检查正在工作,但 ctx.body 不起作用。我如何使用 koa ctx.body 处理多个响应?不知道为什么 ctx.body 不起作用。我们如何解决这个问题?希望你能理解我的问题。
router.post('/userLogin', async (ctx) => {
var email = ctx.request.body.email;
var password = ctx.request.body.password;
if (
!email ||
!password
) {
ctx.response.status = 400;
ctx.body = {
status: 'error',
message: 'Please fill all the fields'
}
} else {
await ctx.app.pool.query("SELECT * FROM users WHERE email = $1",
[`${email}`],
async (err, result) => {
if(err){
console.log(err);
throw err;
}
if (result) {
await bcrypt.compare(password, result.rows[0].password).then(function (res) …Run Code Online (Sandbox Code Playgroud) 我正在使用带有 nodejs 的 Typescript。callback function我有,并且当我控制台时,我正在将数据作为主体中的对象获取。我想在ctx.body. 我可以将其推入object其他variable然后传递给ctx.body吗?
router.post('/api/send_otp', async (ctx: Koa.Context, next: () => Promise<any>) => {
var phone = ctx.request.body.phone;
if (!phone) {
ctx.body = {
message: "Please enter phone number",
};
} else {
var options = {
url: '',
method: 'POST',
auth: {
'user': '',
'pass': ''
},
};
const callback = function(error, response, body) {
if (response) {
console.log(body); // Getting data here
}else{
console.log('error',error);
}
}; …Run Code Online (Sandbox Code Playgroud) 我正在开发披萨订购应用程序,目前我正在尝试实施 Cart。
我为每个用户都有一个购物车,每个购物车包含以下详细信息:cart.ts
import { CartItem } from './cartitem';
export class Cart {
id: string;
user: string;
cartitems: CartItem[];
grand_total: number;
}
Run Code Online (Sandbox Code Playgroud)
用户可以从菜单中添加项目,这将成为我的购物车项目:cartitem.ts
import { Topping } from './topping';
export class CartItem {
id: string;
name: string;
baseprice: number;
toppings: Topping[];
extraprice: number;
quantity= 1;
total: number;
}
Run Code Online (Sandbox Code Playgroud)
因此,在向购物车添加商品时,我在用户购物车上的 API 端点上发出 PUT 请求,以放置购物车商品,作为响应,我将从那里返回购物车内容。
这是我的 cart.component.ts 的样子:
import { Component, OnInit, ViewChild, Inject, Injector } from '@angular/core';
import { CartItem } from '../shared/cartitem';
import { ActivatedRoute } from '@angular/router';
import { …Run Code Online (Sandbox Code Playgroud)