简体   繁体   English

我尝试在我的 mocha nodejs 测试中上传文件,但我得到了 [Object null prototype] { file: {... }}

[英]i try to upload file in my mocha nodejs test but i got [Object null prototype] { file: { ... }}

i find evrywhere solution white:我发现到处都是白色的解决方案:

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

i can use我可以用

JSON.stringify(req.files)

but im sur having a way to fix my problem但我有办法解决我的问题

my mocha test:我的摩卡测试:

it('a file', async function () {
        const body = { pseudo: 'user', password: 'test@123', mail: 'supermail' };

        const response = await fetch(hostName + '/authentication/register', {
            method: 'post',
            body: JSON.stringify(body),
            headers: { 'Content-Type': 'application/json' }
        })
        const usr = await response.json();
        request.post('/usrAction1/doc')
            .field('token', usr.token)
            .attach('file', 'test/test.pdf')
            .end(function (err, res) {
                if (err) {
                    console.log(err)
                }
                console.log(res.status) // 'success' status
            });
    });

and my rout handler:和我的溃败处理程序:

router.post('/doc', async (req, res) => {
        console.log('req.files');
        console.log(req.files)

 })

also my server.js:还有我的 server.js:

import express from 'express'
import authentication from './src/login.js'
import './global/host.js'
import bodyParser from 'body-parser'
import cors from "cors"
import verifyToken from './middleware/auth.js'
import { userAction1, userAction2 } from './src/userAction.js'
import verifyLevel from './middleware/level.js'
import fileUpload from 'express-fileupload';

export default function myApp() {
    const whitelist = [/http:\/\/localhost:*/, /http:\/\/127.0.0.1:*/]

    const corsConfig = { origin: whitelist }

    const app = express();
    const port = hostPort;

    //json encoded
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json())
    app.use(cors(corsConfig))

    // enable files upload
    app.use(fileUpload({
        createParentPath: true
    }));

    app.use('/usrAction1', userAction1())

    app.listen(port, () => {
        console.log(`Example app listening at http://localhost:${port}`);
    });
    return app;
}
myApp();

but don't to work for me.但不要为我工作。 i also test white external client server who juste runing a form and send it to my tested adress and do the same [Object null prototype]我还测试了运行表单的白色外部客户端服务器,并将其发送到我测试的地址并执行相同的操作 [对象 null 原型]

thank u for evry litel help谢谢 evry litel 的帮助

i waiting of some help i using this magique code i found somwhere on stackoverflow:我在等待一些帮助,我正在使用我在 stackoverflow 上某处找到的这个 magique 代码:

req.files && Object.keys(req.files)?.map((obj, idx) => { console.log(req.files['file'].data) })

if somone have a better idea i waiting.如果有人有更好的主意,我会等待。

thank to all感谢所有

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在HTML文件中获取咕gr的摩卡咖啡测试结果? - How can I get my grunt mocha test result in a html file? Nodejs:当我尝试使用 jquery - ajax 上传 img 文件时,req.body 为空 - Nodejs : req.body is empty when i try to upload a img file using jquery - ajax 我测试的文件中的 mocha 访问变量 - mocha access variable in file which function i test 我可以从Mocha单元测试中删除文件吗? - Can I delete a file from inside a mocha unit test? `[对象:null 原型] {标题:'视频',描述:'我的视频!!' } 关于 nodejs - multer` - `[object: null prototype] { title: 'video', description: 'My video!!' } on nodejs - multer` 当我尝试运行mocha测试时,工作人员没有定义 - Worker not defined when I try to run mocha test 使用 jest nodejs 测试多个文件上传 - Test multiple file upload with jest nodejs 我尝试上传大文件时,chrome扩展程序崩溃 - chrome extension crashes when i try to upload a big file 我正在尝试使用node.js将文件上传到AWS,但我不知道我的代码出了什么问题 - I'm try to upload file using node.js to aws but i don't know what went wrong with my code 我想使用nodejs和angularjs上传一个csv文件,然后将其保存到cassandra - i want to upload a csv file using nodejs and angularjs and then save it to cassandra
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM