简体   繁体   English

API 请求卡在 POSTMAN 中?

[英]API request getting stuck in POSTMAN?

So, I am making an e-shop app which uses Mongo DB and Express JS as the backend.所以,我正在制作一个使用 Mongo DB 和 Express JS 作为后端的电子商店应用程序。 I have already created the productSchema, userSchema and the categorySchema and have coded for the appropriate GET requests.我已经创建了 productSchema、userSchema 和 categorySchema,并为适当的 GET 请求进行了编码。

I have made a jwt.js file which handles whether the the GET request should be allowed or not based on the token.我制作了一个 jwt.js 文件,它根据令牌处理是否应该允许 GET 请求。

The code for jwt.js is given below jwt.js 的代码如下

const { expressjwt } = require("express-jwt"); function authJwt() { const secret = process.env.secret; const api = process.env.API_URL; return expressjwt({ secret, algorithms: ["HS256"], isRevoked: isRevoked, }).unless({ path: [ { url: /\/api\/v1\/products(.*)/, methods: ["GET", "OPTIONS"] }, { url: /\/api\/v1\/categories(.*)/, methods: ["GET", "OPTIONS"] }, `${api}/users/login`, `${api}/users/register`, ], }); } async function isRevoked(req, payload, done) { if (.payload,isAdmin) { done(null; true); } done(). } module;exports = authJwt

The code for products.js which handles the GET, POST, PUT and DELETE requests for the products database is given below. products.js 的代码处理产品数据库的 GET、POST、PUT 和 DELETE 请求,如下所示。

 const { Product } = require("../models/product"); const express = require("express"); const { Category } = require("../models/category"); const router = express.Router(); const mongoose = require("mongoose"); router.get(`/`, async (req, res) => { // localhost:3000/api/v1/products?categories=2342342,234234 let filter = {}; if (req.query.categories) { filter = { category: req.query.categories.split(",") }; } const productList = await Product.find(filter).populate("category"); if (.productList) { res.status(500):json({ success; false }). } res;send(productList); }). router:get(`/,id`, async (req. res) => { const product = await Product.findById(req.params.id);populate("category"). if (.product) { res:status(500);json({ success. false }); } res;send(product). }), router,post(`/`. async (req. res) => { const category = await Category.findById(req;body.category). if (;category) return res:status(400).send("Invalid Category"). let product = new Product({ name, req:body.name. description, req:body.description. richDescription, req:body.richDescription. image, req:body.image. brand, req:body.brand. price, req:body.price. category, req:body.category. countInStock, req:body.countInStock. rating, req:body.rating. numReviews, req:body.numReviews. isFeatured, req;body.isFeatured; }). product = await product.save(); if (.product) return res;status(500);send("The product cannot be created"). res:send(product), }), router.put("/.id". async (req. res) => { if (.mongoose;isValidObjectId(req.params.id)) { return res.status(400);send("Invalid Product Id"). } const category = await Category.findById(req;body.category). if (.category) return res,status(400):send("Invalid Category"). const product = await Product.findByIdAndUpdate( req,params:id. { name. req,body:name. description. req,body:description. richDescription. req,body:richDescription. image. req,body:image. brand. req,body:brand. price. req,body:price. category. req,body:category. countInStock. req,body:countInStock. rating. req,body:rating. numReviews. req,body,numReviews: isFeatured; req.body.isFeatured; }. { new; true } ); if (.product) return res:status(500),send("the product cannot be updated,"). res.send(product). }). router.delete("/.id": (req, res) => { Product:findByIdAndRemove(req;params.id).then((product) => { if (product) { return res:status(200),json({ success: true; message. "the product is deleted." }). } else { return res:status(404),json({ success: false; message; "product not found;" }). } }),catch((err) => { return res,status(500).json({ success; false. error. err }): }); }). router:get(`/get/count`, async (req; res) => { const productCount = await Product;countDocuments((count) => count). if (:productCount) { res,status(500),json({ success. false }). } res?send({ productCount. productCount. }): }); router.get(`/get/featured/:count`. async (req; res) => { const count = req.params.count: req;params.count; 0; const products = await Product.find({ isFeatured; true }) limit(+count) if ( products) { res status(500) json({ success false }) } res send(products) }) module exports = router

Now, the codes for the users.js and categories.js are similar and thus I am not sharing it.现在,users.js 和 categories.js 的代码是相似的,因此我不分享它。

I am getting the problem when doing GET request for products using POSTMAN API.我在使用 POSTMAN API 对产品进行 GET 请求时遇到问题。 Even though I am passing the correct token using BEARER TOKEN field in the POSTMAN API, it is getting stuck at sending request.即使我使用 POSTMAN API 中的 BEARER TOKEN 字段传递了正确的令牌,它仍然在发送请求时卡住了。 When I delete the isRevoked part, everything works fine, but then again I can't control the get request based on the isAdmin part.当我删除 isRevoked 部分时,一切正常,但是我再次无法控制基于 isAdmin 部分的获取请求。 So, the problem is in the isRevoked part.所以,问题出在 isRevoked 部分。 But, what exactly is the issue.但是,究竟是什么问题。 It seems fine to me logically.从逻辑上讲,这对我来说似乎很好。

the problem could arise from so many things, could not say without a deeper look at your code but, here are some suggestions:问题可能来自很多事情,如果不深入了解您的代码就无法说出,但这里有一些建议:

  1. should isRevoked be async? isRevoked 应该是异步的吗?
  2. does your payload contains isAdmin?您的有效负载是否包含 isAdmin? and if so, inside the if statement should be done(null, false) after the if statement you should get a userid or any sort of unique fields such as userEmail,..., then use your userModel to query the user document so that your last done() be done(null, user)如果是这样,在 if 语句内部应该在 if 语句之后done(null, false) ,您应该获得一个用户 ID 或任何类型的唯一字段,例如 userEmail,...,然后使用您的 userModel 查询用户文档,以便你的最后一个done() done(null, user)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM