发布请求,返回未定义,NodeJS,Express

时间:2020-10-14 05:07:03

标签: javascript node.js http-post postman body-parser

当我在邮递员请求的正文中向其传递json对象时,我的发布请求返回未定义。我一直在试图弄清楚为什么会发生这种情况,但是在第三个小时,我似乎无法查明出什么问题了。

以下是相关代码:

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

关于邮递员,这是我要插入的内容: 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)


1 个答案:

答案 0 :(得分:0)

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

以这种方式进行操作,可以解决您的问题。

const { Dblock } = req.body;在这里,您的代码正在Dblock中寻找名为req.body的属性。

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