【问题标题】:Form-Data POST not being recieved in server but Postman is working for RAW JSON表单数据 POST 未在服务器中收到,但邮递员正在处理 RAW JSON
【发布时间】:2020-04-21 13:18:10
【问题描述】:

当我使用表单发送请求时,正文对象不包含所需的电子邮件值..

例如当我使用邮递员时:

这个错误是一个架构错误,因为电子邮件没有通过......

我曾尝试使用 bodyparser 解析标头,但没有运气..

但是当我使用原始 JSON 发送它时它工作正常:

我的根服务器路由:

const express = require('express');
const app = express();
const mongoose = require('mongoose');


var cors = require('cors');

app.use(cors());

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();
});


// parse body on every request //
app.use(express.json());
app.use(express.urlencoded({ extended: false }));


app.use('/api/post', require('./routes/postRoute'));
app.use('/api/users', require('./routes/usersRoute'));


// root home page
app.get('/', (req, res) => {
    res.send('hi, love you ok? bye');
});

// connect to DB //

mongoose
    .connect('mongodb://localhost:27017/Express_Sessions_Wireframe', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true
    })
    .then(console.log('We have touchdown houston [DATABASE HAD CONNECTION]'))
    .catch((err) => console.log(err));

// listen to server
app.listen(3000, () => {
    console.log('roger roger [server listening]');
});

我的用户架构:

let User = new Schema(
    {
        email: {
            type: String,
            required: true,
            unique: true
        },
        posts: [
            {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Post'
            }
        ]
    },
    {
        timestamps: true
    }
);

module.exports = mongoose.model('User', User);

我的发帖路线

const express = require('express');
const User = require('../models/userModel');


var router = express.Router();

// parse json to use in all requests got or sent by router .. which is provided by express
router.use(express.json());


// User can signUp for an account
router.post('/signup', (req, res, next) => {
                User.create({
                    email: req.body.email
                })
        .then(
            (user) => {
                res.statusCode = 200;
                console.log( 'Registration Successful!', `user ====== ${user}` )
                res.setHeader('Content-Type', 'application/json');
                res.json({ status: 'Registration Successful!', user: user });
            },
            (error) => next(error)
        )
        .catch((err) => next(err));
});



module.exports = router;

【问题讨论】:

    标签: node.js reactjs mongodb express mongoose


    【解决方案1】:

    mutlipart/form-data 不由 bodyparser 库处理,如下所述: https://www.npmjs.com/package/body-parser#readme

    还有其他库可以帮助解析 multipart/form-data。它们在上面的链接中注明。这是摘录...

    这不处理多部分实体,因为它们复杂且通常很大。对于多部分正文,您可能对以下模块感兴趣:

    busboy 和 connect-busboy

    多方和连接多方

    厉害

    混合器

    【讨论】:

    • 这不可能是问题,我也试过 app.use(express.json()) ,也没有运气..
    • @jcx3x 但是,您在 express 中处理多部分表单数据的方式是使用 3rd 方库,例如 multer。没有它,当它作为多部分表单数据发送时,您将无法获得电子邮件地址。使用 app.use(express.json()) 不适用于表单数据。您可以在此处查看如何使用 Multer 的示例:stackoverflow.com/questions/37630419/…
    • 嗯,是的,我认为你是对的......但我的 formdata 仍然令人遗憾地返回一个空对象......即使使用 multer。
    • @jcx3x 您可以使用 multer 编辑您的问题并包含您的代码吗?我很乐意看一下,看看是否能发现问题。
    • @jcx3x 太好了!乐意效劳!新年快乐。
    猜你喜欢
    • 1970-01-01
    • 2015-10-24
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2019-06-09
    • 2014-09-13
    相关资源
    最近更新 更多