1

我建立了一个电子商务网站,并使用 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;

问题是当我这样使用我的发布请求时

router.post('/host', upload.single('photos'), controllpages.hostPost);

照片不会保存到我的数据库中,只有来自 controllpages.hostPost 的产品信息会。

当我将发布请求与任何其他功能一起使用时

router.post('/host', upload.single('photos'), (req , res) => {
  console.log('Done')
};

照片确实保存到我的数据库中,但我丢失了产品信息。

我应该怎么办?

4

0 回答 0