【问题标题】:Unable to post to json data无法发布到 json 数据
【发布时间】:2020-09-13 19:12:14
【问题描述】:

我在尝试将图像与 json 数据一起发布时遇到此错误。 错误:参数“data”的值不是有效的 Firestore 文档。不能使用“未定义”作为 Firestore 值(在“价格”字段中找到)在此处输入图像描述

   let imageUrl;

   const BusBoy = require('busboy');
   const path = require('path');
   const os = require('os');
   const fs = require('fs');

   const busboy = new BusBoy({ headers: req.headers });

   let imageToBeUploaded = {};
   let imageFileName;

   busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
     console.log(fieldname, file, filename, encoding, mimetype);
     if (mimetype !== 'image/jpeg' && mimetype !== 'image/png') {
       return res.status(400).json({ error: 'Wrong file type submitted' });
     }
     // my.image.png => ['my', 'image', 'png']
     const imageExtension = filename.split('.')[filename.split('.').length - 1];
     // 32756238461724837.png
     imageFileName = `${Math.round(
       Math.random() * 1000000000000
     ).toString()}.${imageExtension}`;
     const filepath = path.join(os.tmpdir(), imageFileName);
     imageToBeUploaded = { filepath, mimetype };
     file.pipe(fs.createWriteStream(filepath));
   });
   busboy.on('finish', () => {
     admin
       .storage()
       .bucket(`${config.storageBucket}`)
       .upload(imageToBeUploaded.filepath, {
         resumable: false,
         metadata: {
           metadata: {
             contentType: imageToBeUploaded.mimetype
           }
         }
       })
       .then(() => {
         imageUrl = `https://firebasestorage.googleapis.com/v0/b/${
           config.storageBucket
         }/o/${imageFileName}?alt=media`;
         const newProduct = {
             description: req.body.description,
             price: req.body.price,
             title: req.body.title,
             createdAt: new Date().toISOString(),
             likeCount:0,
             reviewCount:0,
             orderCount: 0,
             storeName: req.user.userName,
             sellerImage: req.user.imageUrl,
             imageUrl: imageUrl,
         };

         db .collection('products')
             .add(newProduct)
             .then(doc => {
                 const resProduct = newProduct;
                 resProduct.productId = doc.id;
                 res.json(resProduct);
             })

       })
       .then(() => {
         return res.json({ message: 'product uploaded successfully' });
       })
       .catch((err) => {
         console.error(err);
         return res.status(500).json({ error: 'something went wrong' });
       });
   });
   busboy.end(req.rawBody);
};

postman 中是否可以同时发布文件和 json 数据?

【问题讨论】:

  • 我得到的错误是:错误:参数“数据”的值不是有效的 Firestore 文档。不能使用“未定义”作为 Firestore 值(在“价格”字段中找到)

标签: node.js firebase firebase-realtime-database google-cloud-firestore google-cloud-functions


【解决方案1】:

错误消息告诉您,您有一个名为“价格”的字段,其值未定义。 Undefined 不是有效的 Firestore 值。你必须弄清楚为什么会这样以及它应该是什么。检查req.body.price 的值。如果你不能给它一个合适的值,把它从对象中移除。

【讨论】:

  • 感谢您的帮助。当我从它工作的对象中删除这个description: req.body.description,price: req.body.price,title: req.body.title, 时。但是有没有办法将这些值添加到对象中。
  • 当然,正如我所说,该值是未定义的。您需要确保该值对 Firestore 有效。我们不知道值的来源,因此您必须确定这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多