【问题标题】:Object: null prototype issue in node js [Object: null prototype] { title: 'book' }对象:节点js中的空原型问题[对象:空原型] {标题:'书'}
【发布时间】:2020-03-14 14:59:12
【问题描述】:

我对节点 js 很陌生 我已创建代码

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

const app = express();

app.use(bodyParser.urlencoded({extended: false}));

app.use('/add-product' ,(req, res, next) => {
    res.send('<form action="/product" method="POST"><input type="text" name="title"><button type="submit">Submit</button></form>');
});

app.use('/product' ,(req, res, next) => {
    console.log(req.body);
    res.redirect('/');
});

app.use('/' ,(req, res, next) => {
    res.send("<h1>Hello Express!</h1>");

});
app.listen(3000); 

它给了我输出

[Object: null prototype] { title: 'book' }

输出应该是

{ title: 'book' }

我试过这个how to fix [Object: null prototype] { title: 'product' },但它对我不起作用。 尝试链接错误截图:https://www.screencast.com/t/0p9rtUTAsL

请帮帮我!

【问题讨论】:

  • 为什么输出对您来说是个问题?它只是告诉你对象的原型是null。除非您要求原型是特定值,否则这无关紧要。
  • 您说您尝试了其他问题的某些内容,但接受的答案清楚地表明这不是问题并解释了原因。这使您的问题更加令人困惑。

标签: javascript node.js express


【解决方案1】:

还要添加bodyParser.json() 以创建“application/json”解析器,如下所示:

app.use(boydParser.urlencoded({extended: false}));
app.use(boydParser.json());

【讨论】:

    【解决方案2】:

    这是您想要的输出的解决方案:

    app.use(bodyParser.urlencoded({extended: true}));
    

    【讨论】:

      猜你喜欢
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      相关资源
      最近更新 更多