当前位置: 首页 > 知识库问答 >
问题:

TypeError:无法对“req”的属性“userId”进行解构。主体“未定义”

薛弘阔
2023-03-14

每次我试图在邮递员http://localhost:3000/api/orders/new

我得到了这个错误:**TypeError:无法解构属性'userId'的req.body',因为它是未定义的。在C:\用户\Web-Developer\Desktop\shop py\backend\路由\orders.js:70: 10在Layer.handle[作为handle_request](C:\用户\Web-Developer\Desktop\shop py\backend\node_modules\Express\lib\router\layer.js:95: 5)在下一个(C:\用户\Web-Developer\Desktop\shop py\backend\node_modules\Express\lib\router\route.js:137: 13)在Route.dispatch(C:\用户\Web-Developer\Desktop\shop py\backend\node_modules\Express\lib\router\route.js:112: 3)在Layer.handle[作为handle_request](C:\用户\Web-Developer\Desktop\shop py\backend\node_modules\lib\router\layer.js:95: 5)在C:\用户\Web-Developer\Desktop\shop py\backend\node_modules\lib\router\index.js:281: 22在Function.process_params(C:\用户Desktop\shop py\backend\node_modules\Express\lib\router\index.js:335: 12)在下一个(C:\User\Web-Developer\Desktop\shop py\backend\node_modules\Express\lib\router\index.js:275: 10)在Function.handle(C:\User\Web-Developer\Desktop\shop py\backend\node_modules\Express\lib\router\index.js:174: 3)在路由器(C:\User\Web-Developer\Desktop\shop py\backend\node_modules\Express\lib\router\index.js:47: 12)**

const router = express.Router();
const {database} = require('../config/helpers');

/* GET ALL ORDERS */
router.get('/', (req, res) => {
    database.table('orders_details as od')
        .join([
            {
                table: 'orders as o',
                on: 'o.id = od.order_id'
            },
            {
                table: 'products as p',
                on: 'p.id = od.product_id'
            },
            {
                table: 'users as u',
                on: 'u.id = o.user_id'
            }
        ])
        .withFields(['o.id', 'p.title as name', 'p.description', 'p.price', 'u.username'])
        .sort({id: 1})
        .getAll()
        .then(orders => {
            if(orders.length > 0) {
                res.status(200).json(orders);
            } else {
                res.json({message: 'Mo Orders Found'})
            }
        }).catch(err => console.log(err));
})


/* GET SINGLE ORDER */
router.get('/:id', (req, res) => {

    const orderId = req.params.id;

    database.table('orders_details as od')
        .join([
            {
                table: 'orders as o',
                on: 'o.id = od.order_id'
            },
            {
                table: 'products as p',
                on: 'p.id = od.product_id'
            },
            {
                table: 'users as u',
                on: 'u.id = o.user_id'
            }
        ])
        .withFields(['o.id', 'p.title as name', 'p.description', 'p.price', 'u.username'])
        .filter({'o.id': orderId})
        .getAll()
        .then(orders => {
            if(orders.length > 0) {
                res.status(200).json(orders);
            } else {
                res.json({message: `No Orders Found with orderId ${orderId}`})
            }
        }).catch(err => console.log(err));
})

/* PLACE A NEW ORDER */
router.post('/new', (req, res) => {

    let {userId, products} = req.body;
    console.log(userId, products)
})

module.exports = router

共有3个答案

皇甫繁
2023-03-14

您可能缺少bodyParser:

const-bodyParser=require('body-parser')

<代码>应用程序。使用(bodyParser.json())//解析应用程序/json

刘焱
2023-03-14
app.use(bodyParser.json()) // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true }))

我发现在指定路由路径之前需要使用此代码。

文喜
2023-03-14

以下是解决方案。。。耶!!

不需要安装主体解析器。

尝试以下操作:

app.use(express.json())

并在您的路线之前指定它,例如:

(这里的顺序很重要!)

app.use(express.json());
app.use('/api', Anyroute)
 类似资料:
  • (index.js) const express=require('express');const cors=需要(“cors”); 需要('dotenv'). config({path: '. /.env'}); const createCheckout会话=需要('./api//check out'); const app=express(); const端口=8080; app.use(ex

  • 问题内容: 我试图在ajax回调从REST api接收数据后设置组件的setState。这是我的组件构造函数代码 然后,我有一个如下所示的方法。 现在,这是我执行getAJAX请求的getPosts函数。 我想设置状态,但出现以下错误。 不太清楚是什么原因造成的。如果有人指出我正确的方向,那将真的很有帮助。提前致谢。 问题答案: 还绑定回调函数,以便回调内部指向React Component的上下

  • 我试图使用node.js express web服务器中的@tensorflow/tfjs-node模块。但是,我得到了下面的错误。我不明白为什么我会出现这个错误。我刚刚在node.js服务器中添加了1行代码。我使用“npm install@tensorflow/tfjs-node”完成的安装。可能的问题是什么? null 先谢谢你, var nonMaxSuppressionV3Impl=tf.

  • 我正在尝试联系许多对许多,在两个模式“培训和培训课程”,所以我做了第三个模式“课程”,以尝试关系1对许多。但是我得到了一个错误: null TypeError:无法读取对象上未定义得属性“belongsto”。(c:UsersDellDownloadsGraphql-12.18.2020Graphql-12.18.2020Graphql-12.18.2020ServerAppDatabaseDat

  • 上面的函数返回一个类似“failed”的字符串。但是,当我尝试在其上运行then函数时,它将返回 并且光标指示在之后和。 下面是完整的功能: 更新 下面是函数 我确信将导致一个“failed”字符串。没别的了。

  • 无法显示Barchart。 我有一个包含数据的表,它是可拖动的。在表格旁边我有一个两个dropbox被拖动的表格中的数据可以被拖动到框中。当将数据拖动到框中时,应该会显示条形图。 这是我的家。component.html 这是我的家。Component.TS 这是我的酒吧 在拖放数据时,特定的图表必须显示在Dropbox上。