【问题标题】:Form POST request, not working. Undefined in Server (HTML,JS,EXPRESS,NODE,MONGODB)表单 POST 请求,不起作用。服务器中未定义(HTML、JS、EXPRESS、NODE、MONGODB)
【发布时间】:2022-01-20 21:14:52
【问题描述】:

我在一个有前端和后端的应用程序中工作,我的后端是用 nodejs 编写的,使用 express、multer 和 mongoose,当我通过邮递员发送数据时工作得很好,但是一旦我尝试相同的请求在我用 html、css 和 vanilla javascript 制作的客户端中,它不起作用,它给了我以下错误:

TypeError: Cannot read property 'path' of undefined(指向我的管理特定实体连接的网络文件)。

尽管有上面提到的错误,我一直在尝试将一个文件和一个字符串从 html 表单发送到我的节点服务器中的网络文件,但我在终端中不断收到大约 2 个相同的错误。

这里有一些代码,你可以更好地理解。

server.js(主入口点)

const express = require('express');
const app = express();
const server = require('http').Server(app);
const path = require('path');

const cors = require('cors');
const bodyParser = require('body-parser');
const db = require('./db');
const router = require('./network/routes');
const config = require('./config');

db(config.dbUrl);
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(`${config.publicRoute}`, express.static(path.join(__dirname, 'public')));
server.listen(config.port, function() {});
console.log(`Application listen on ${config.host}${config.port}`);
router(app);

network.js(管理“横幅”实体连接的文件)

const express = require('express');
const multer = require('multer');
const router = express.Router();
const response = require('../../network/response');
const controller = require('./controller');
const path = require('path');
const storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, path.join(__dirname, '../../public/files'))
    },
    filename: function(req, file, cb) {
        cb(null, path.extname(file.originalname))
    }
})
const upload = multer({ storage: storage })

router.post('/', upload.single('file'), function(req, res) {

    console.log(req.body);
    controller.addBanner(req.body.name, req.file, req.file.path)
        .then((fullMessage) => {
            response.success(req, res, fullMessage, 201);
        })
        .catch(e => {
            response.error(req, res, 'Unexpected error', "Simulated Error", 400, e);
        });
});

router.get('/', function(req, res) {
    controller.getBanners()
        .then((banners) => {
            response.success(req, res, banners, 200);
        })
        .catch(e => {
            response.error(req, res, 'Internal Error', 500, e);
        })
});

router.delete('/:id', function(req, res) {
    controller.deleteBanner(req.params.id)
        .then(() => {
            response.success(req, res, `Imagen con id: ${req.params.id} ,eliminada`, 200);
        })
        .catch(e => {
            response.error(req, res, 'Internal Error', 500, e);
        })
});

module.exports = router;

panel.html(假定发送 post 请求的表单所在的位置)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- <meta http-equiv="Content-Security-Policy" content="default-src https://cdn.example.net; child-src 'none'; object-src 'none'"> -->
    <link rel="stylesheet" type="text/css" href="css/panel-style.css">
    <script defer src="panel-script.js"></script>
    <title>Panel de Control</title>
</head>

<body>
    <header>
        <img src="assets/mrvapeslogo.png" alt="mrvapes logo">
        <a href="index.html"></a>
    </header>
    <section>
        <form accept-charset="UTF-8" enctype="multipart/form-data" action="../components/banner/network.js" autocomplete="off" method="GET" target="_blank">
            <label for="user">Banners Activos</label><br/>
            <ul class="files-container">

            </ul>
        </form>
        <form accept-charset="UTF-8" enctype="multipart/form-data" action="http://localhost:3000/banner" autocomplete="off" method="POST" target="_blank">
            <div class="container">
                <div class="button-wrap">
                    <!-- <label class="button" for="upload">Subir</label> -->
                    <input type="text" id="name" placeholder="Nombre de Archivo" value="" name="name" required>
                    <input id="image" value="Subir" placeholder="Subir Archivo" type="file" required>
                    <button id="upload" name="send-post--request" value="post-request" type="submit">Enviar</button>
                    <!-- <input id="upload" type=" submit " value="Enviar " onclick="submit() "> -->
                </div>
            </div>
        </form>
    </section>
</body>

panel-script.js(管理 http 请求逻辑的客户端文件)

const upload = document.getElementById("upload");
const image = document.getElementById("image");
const title = document.getElementById("name");

upload.addEventListener("click", (e) => {
    console.log('im in bro');
    e.preventDefault();
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
            alert(xhr.response);
        }
    }
    xhr.open("POST", 'http://localhost:3000/banner', true);
    //xhr.setRequestHeader("Accept", "multipart/form-data");
    //xhr.setRequestHeader('Content-Type', 'multipart/form-data');
    var fileSent = {
        "name": title,
        "file": image
    };
    console.log(fileSent);
    xhr.send(fileSent);
    alert('Subida exitosa!');
})


function retrieve() {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
            alert(xhr.response);
        }
    }
    xhr.open('get', 'http://localhost:3000/banner', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    xhr.send();
}

我非常愿意听取您的建议,在此先感谢各位开发者。

【问题讨论】:

    标签: javascript html node.js express httprequest


    【解决方案1】:

    您需要为&lt;input type="file"&gt; 提供name。它必须匹配upload.single('file')

    <input id="image" name="file" value="Subir" placeholder="Subir Archivo" type="file" required>
    

    默认情况下,在 Express 4 req.file 在 req 对象上不再可用。我看到你正在使用 multer 中间件来处理这个问题。当您尝试访问req.file.path 时,错误可能来自network.js 中的multer。 req.file 未定义,因为 upload.single('file') 正在寻找名为 file 的表单数据,而表单输入中没有 name="file"

    -编辑-

    我现在看到您没有尝试使用本机 HTML 表单提交发送文件(现在应该可以使用我的答案),并且您正在收听提交按钮点击以在 panel-script.js 中使用 XMLHttpRequest 发送。仅 XHR FormData API 支持使用 XMLHttpRequest 发送 multipart/form-data(仅适用于新浏览器)。如果您想继续使用panel-script.js,请在此处查看答案:Send a file as multipart through XMLHttpRequest

    在您的情况下,您必须使用:

    var formData = new FormData();
    formData.append("file", document.getElementById("image").files[0]);
    
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "http://localhost:3000/banner");
    xhr.send(formData);
    

    【讨论】:

    • 对我没用,只是添加了 name="file",但它给了我相同的未定义路径错误
    • 那么看来你有两个问题。错误在哪一行?
    • 问题已解决,目前正在将图像从数据库中提取到我的html中
    • 我明白了。我不知道panel-script.js 不知道我是怎么错过的。你是最近加的吗?现在我看到您正在尝试使用 XMLHttpRequest 发送,请参阅上面我编辑的答案。
    【解决方案2】:

    刚刚解决了问题,加上我从 DSander 同事那里得到的回复,我实际上需要将名称放在我的 html 标签中,我不知道它是否正确,但解决问题的是删除我的html中的脚本标签,所以它不使用我写的发送方法。

    评论了这个

    <script defer src="panel-script.js"></script>
    

    并停止使用 panel-script.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-13
      • 2021-09-19
      • 2014-06-29
      • 1970-01-01
      • 2018-02-10
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多