Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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
Node.js 如何将产品表单和图片上传到MongoDB atlas服务器?_Node.js_File Upload_Fetch_Multer_Multer Gridfs Storage - Fatal编程技术网

Node.js 如何将产品表单和图片上传到MongoDB atlas服务器?

Node.js 如何将产品表单和图片上传到MongoDB atlas服务器?,node.js,file-upload,fetch,multer,multer-gridfs-storage,Node.js,File Upload,Fetch,Multer,Multer Gridfs Storage,我建立了一个电子商务网站,并使用fetch函数将产品数据提取到后端。像这样 const form = document.querySelector('form'); form.addEventListener('submit', async(e) => { e.preventDefault(); // get data from infront end const title = form.title.value; const category = form.category.v

我建立了一个电子商务网站,并使用fetch函数将产品数据提取到后端。像这样

const form = document.querySelector('form');


form.addEventListener('submit', async(e) => {

e.preventDefault();

// get data from infront end

const title = form.title.value;
const category = form.category.value;
const hostKind = form.hostingKind.value;
const area = form.area.value;
const realStateDesc = form.realstateDisc.value;
const neighbourhoodDesc = form.neighbourhood.value;
const governorate = form.governorate.value;
const city = form.city.value;
const address = form.address.value;
const zipCode = form.zipCode.value;
const peopleAllowed = form.allowed.value;
const peopleMaxNum = form.maxnum.value;
const timeper = form.timeper.value;
const additional = form.additional.value;
const price = form.rentprice.value;
const photos = form.photos.value;

fetch(photos).then(response => {
    console.log(response);
    return response.blob();
})



//fetch data
try {

    const res = await fetch('/host', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({

            title: title,
            category: category,
            hostKind: hostKind,
            area: area,
            realStateDesc: realStateDesc,
            neighbourhoodDesc: neighbourhoodDesc,
            governorate: governorate,
            city: city,
            address: address,
            zipCode: zipCode,
            peopleAllowed: peopleAllowed,
            peopleMaxNum: peopleMaxNum,
            timeper: timeper,
            additional: additional,
            price: price

        })
    });

    //location.assign('/congrats');


} catch (err) {
    console.log(err)
};






});
然后我使用GridFS和Multer将表单中的图像上传到我的MongoDB服务器,就像这样

mongoose.connect(process.env.DB_CONNECT, { useNewUrlParser: true, useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true, })
.then((result) => app.listen(3000))
.catch((err) => console.log(err));
const conn = mongoose.connection;


//init gfs
let gfs;

//const storage = new GridFSStorage({ db: conn });

conn.once('open', () => {
    gfs = Grid(conn.db, mongoose.mongo);
    gfs.collection('uploads');
});

//create storage engine

const storage = new GridFsStorage({
    options: { useUnifiedTopology: true },
    url: process.env.DB_CONNECT,
    file: (req, file) => {
        return new Promise((resolve, reject) => {
            crypto.randomBytes(16, (err, buf) => {
                if (err) {
                    return reject(err);
                }

                const filename = buf.toString('hex') + path.extname(file.originalname);
                const fileInfo = {
                    filename: filename,
                    bucketName: 'uploads',

                };
                resolve(fileInfo);
            });
        });
    }
});




const upload = multer({ storage });
module.exports = upload;
问题是当我这样使用我的post请求时

router.post('/host', upload.single('photos'), controllpages.hostPost);
照片不会保存到我的数据库中,只有ControlPages.hostPost中的产品信息会保存

当我将post请求与其他类似的函数一起使用时

router.post('/host', upload.single('photos'), (req , res) => {
  console.log('Done')
};
照片确实保存到我的数据库中,但我丢失了产品信息

我该怎么办