Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS-使用Axios和/或FormData发送存储在MongoDB中的文件_Javascript_Node.js_Mongodb_Express_Axios - Fatal编程技术网

Javascript NodeJS-使用Axios和/或FormData发送存储在MongoDB中的文件

Javascript NodeJS-使用Axios和/或FormData发送存储在MongoDB中的文件,javascript,node.js,mongodb,express,axios,Javascript,Node.js,Mongodb,Express,Axios,我一直在努力完成以下任务 在我的后端,我希望能够检索存储在MongoDB中的文件,然后使用AXIOS或FormData将该文件提交到外部API 我正在使用NodeJS、Express、MongoDB、Mongoose、multer、multer-gridfs存储 我知道我必须使用mongo.GridFSBucket来获取文件块。以下是代码的一部分: try { const formData = new FormData() const conn = mo

我一直在努力完成以下任务

在我的后端,我希望能够检索存储在MongoDB中的文件,然后使用AXIOS或FormData将该文件提交到外部API

我正在使用NodeJS、Express、MongoDB、Mongoose、multer、multer-gridfs存储

我知道我必须使用mongo.GridFSBucket来获取文件块。以下是代码的一部分:

    try {
        const formData = new FormData()

        const conn = mongoose.createConnection('mongodb://localhost:27017/mhov', {
            useNewUrlParser: true,
            useUnifiedTopology: true
        });

        await conn.once("open", () => {
            // init stream
            const gfs = new mongoose.mongo.GridFSBucket(conn.db, {
                bucketName: "uploads"
            });
        });

        const file = await gfs.find({ filename: req.files[0].filename }).toArray((err, files) => {
            if (!files || files.length === 0) {
                console.log('NO FILES FOUND')
                return 'NO FILES'
            } else {
                console.log('FILE FOUND')
                console.log(files[0])
                
                // HERE IS WHAT I DON'T KNOW WHAT TO DO.
                formData.append('file', gfs.openDownloadStreamByName(files[0].filename).pipe());

                // return files[0]
                // console.log(files)
            }
            // console.log('MY FILE NAME ', files[0].filename)
            // formData.append('file', gfs.openDownloadStreamByName(files[0].filename).pipe())
            // gfs.openDownloadStreamByName(myFiles[0].filename).pipe()
        })
        console.log('NO LO CREO')
        // await console.log(file)


    } catch (err) {
        console.log('ERROR')
        console.log(err)
    }
}```

So basically I don't know how to append the file to the formData...

Any help would be greatly appreciated. 

Thanks

你能谈谈你的问题吗?