【问题标题】:req.body is returning a default value and not my POST requestreq.body 返回默认值,而不是我的 POST 请求
【发布时间】:2019-12-31 07:30:10
【问题描述】:

我确定我犯了一个小错误,我无法在其他问题中找到答案。基本上每次我尝试发出 POST 请求时,控制台都会显示“{}”而不是来自 POST 请求的值。我正在使用 EJS 和 Node.js 来处理这个 POST 请求。

这是来自 ejs 文件的代码

<label> My Favourite Saying</label>
    <form action ="/users/dashboard" method ="POST">
        <div class = "form-group">
        <input 
        type="name" 
        id="favSaying" 
        class="form-control"
        placeholder = "Enter phrase here"
        />
        </div>
        <small id="FavSayingInst" class="form-text text-muted">
            Enter a phrase to have it stored for the next time you login.
        </small>
    <button type="submit" class="btn btn-dark mt-2">Submit</button>
</div>
</form>

这是处理程序文件中的代码

//Handle the fav saying
router.post('/dashboard', (req,res) => {
  console.log(req.body);
  console.log("test123");
  //User.findOneAndUpdate({email:'tester2@gmail.com', favSaying})
  req.flash('store_msg', 'Your phrase has been stored!');
  res.redirect('/views/dashboard');
});

我已经添加了正文解析器,所以我知道不是这样,:

const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const app = express();
const mongoose = require('mongoose');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');

require('./config/passport')(passport);

//EJS
app.use(expressLayouts);
app.set('view engine','ejs');

//DB Config
const db = require('./config/keys').MongoURI;

//Connect to Mongo
mongoose.connect(db, { useNewUrlParser: true })
.then(() => console.log('MongoDB connected...'))
.catch(err => console.log(err));

//Bodyparser
app.use(express.urlencoded({extended: false}));

//Session
app.use(session({
    secret: 's',
    resave: true,
    saveUninitialized: true
}));

//Passport
app.use(passport.initialize());
app.use(passport.session());

//Connect flash
app.use(flash());

//Vars
app.use((req,res,next) => {
    res.locals.success_msg = req.flash('success_msg');
    res.locals.error_msg = req.flash('error_msg');
    res.locals.error = req.flash('error');
    res.locals.store_msg = req.flash('store_msg');
    next();
});

//Routes
app.use('/', require('./routes/index2')); //The first comment is where the webpage routes to, the second is the js file
app.use('/users', require('./routes/users'));

const PORT = process.env.PORT || 5000; 

app.listen(PORT, console.log(`Server started on Port  ${PORT}`));


有人知道为什么我会得到“{}”而不是我的价值观吗?我有

【问题讨论】:

  • 你的路径正确吗? "/users/dashboard"
  • @Kaasim Shaikh 你能发布你的server.js 代码吗??
  • @ManjeetThakur 我想是的,当我单击按钮时,Visual Studio 中的控制台会打印出“test123”
  • @KaasimShaikh 你想要什么response
  • 对于req.body,您得到的是空对象?

标签: javascript node.js ejs


【解决方案1】:

输入元素应该有一个名称属性来将数据传输到请求中。

【讨论】:

  • 这意味着我要睡觉了……哈哈哈非常感谢。这解决了问题
【解决方案2】:

您的输入标签中应该有 'name' 属性。

【讨论】:

    【解决方案3】:

    您需要检查您的response

    // POST method route
    router.post('/', function (req, res) {
      res.send(req.body);
    });
    

    试试看。

    你必须解决这个问题。

    <input type="text" name="name" ...>
    

    这就是为什么&lt;input&gt; 标签类型没有name
    并且名称是post body 字段键。

    【讨论】:

    • 这将返回与 console.log 相同的内容,即“{}”
    • @KaasimShaikh 好的,我明白了。等我
    • @KaasimShaikh 解决这个问题。
    • 是的,那是我的错字。还是不行。
    • @KaasimShaikh ..... 我已经这样做了。名称属性..;;
    猜你喜欢
    • 2021-03-08
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 2023-03-11
    • 2016-09-09
    • 2023-04-05
    相关资源
    最近更新 更多