代码之家  ›  专栏  ›  技术社区  ›  Vaishali

为什么我在后台收到错误“Route.post()需要回调函数,但得到了一个[object Undefined]”?

  •  0
  • Vaishali  · 技术社区  · 2 年前

    我正在建立一个电子商务网页,现在正在处理支付部分。我已经创建了控制器和路由器,但出现了错误

    Error: Route.post() requires a callback function but got a [object Undefined]
        at Route.<computed> [as post] (F:\zebrow_mern\node_modules\express\lib\router\route.js:211:15)
        at Object.<anonymous> (F:\zebrow_mern\backend\routes\paymentRouter.js:9:34)
        at Module._compile (node:internal/modules/cjs/loader:1159:14)
    

    这是我的控制器

    const catchAsyncErrors = require("../middleware/catchAsyncError");
    
    const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
    
    const processPayment = catchAsyncErrors(async (req, res, next) => {
      const myPayment = await stripe.paymentIntents.create({
        amount: req.body.amount,
        currency: "inr",
        metadata: {
          company: "Zebrow",
        },
      });
    
      res
        .status(200)
        .json({ success: true, client_secret: myPayment.client_secret });
    });
    
    exports.sendStripeApiKey = catchAsyncErrors(async (req, res, next) => {
      res.status(200).json({ stripeApiKey: process.env.STRIPE_API_KEY });
    });
    module.exports = {processPayment}
    
    

    路由器文件

    const express = require("express");
    const {
      processPayment,
      sendStripeApiKey,
    } = require("../controllers/paymentController");
    const router = express.Router();
    const { isAuthenticatedUser } = require("../middleware/auth");
    
    router.route("/payment/process").post(isAuthenticatedUser, processPayment);
    
    router.route("/stripeapikey").get(isAuthenticatedUser, sendStripeApiKey);
    
    module.exports = router;
    
    0 回复  |  直到 2 年前
    推荐文章