Post 请求,返回未定义,NodeJS,Express

Post Request, Returning Undefined, NodeJS, Express

我的 post 请求返回 undefined 当我在 postman 请求的正文中传递一个 json 对象给它时。我一直在试图弄清楚为什么会发生这种情况,但在第三个小时我似乎无法确定问题所在。

相关代码如下:

app.post('/api/createBlock', (req, res) =>{
    const { Dblock } = req.body;
    console.log(Dblock);
    //Dblock returning undefined
    //dataBlock.createDB(Dblock);
});

至于 postman,这就是我要插入的内容: json object to a post request at the right endpoint

这里是错误:

TypeError: Cannot read property 'auth' of undefined
    at /home/main/public_html/Cypher-Network/src/index.js:174:35
    at Layer.handle [as handle_request] (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/layer.js:95:5)
    at /home/main/public_html/Cypher-Network/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/index.js:335:12)
    at next (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/index.js:275:10)
    at /home/main/public_html/Cypher-Network/src/index.js:60:3
    at Layer.handle [as handle_request] (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/layer.js:95:5)
TypeError: Cannot read property 'auth' of undefined
    at /home/main/public_html/Cypher-Network/src/index.js:174:35
    at Layer.handle [as handle_request] (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/layer.js:95:5)
    at /home/main/public_html/Cypher-Network/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/index.js:335:12)
    at next (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/index.js:275:10)
    at /home/main/public_html/Cypher-Network/src/index.js:60:3
    at Layer.handle [as handle_request] (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/layer.js:95:5)


app.post('/api/createBlock', (req, res) =>{
  const Dblock = req.body;
  console.log(Dblock);
  //Dblock returning undefined
  //dataBlock.createDB(Dblock);
});

这样操作,就可以解决你的问题了。

const { Dblock } = req.body; 此处,您的代码正在寻找 req.body 中名为 Dblock 的 属性,但该

不可用。

要了解有关解构赋值的更多信息,请查看here