簡體   English   中英

SyntaxError: Unexpected token - JSON 中的位置 0(Firebase,Busboy)

[英]SyntaxError: Unexpected token - in JSON at position 0(Firebase,Busboy)

我正在嘗試將圖像上傳到 firebase 上的用戶,但我不知道為什么在向服務器發送請求時出現此錯誤(語法錯誤:意外令牌 - 在位置 0 的 JSON 中)

此外,這是一條受保護的路線(用戶需要先登錄/注冊),但如果用戶未登錄,我應該收到“未授權”之類的錯誤(就像我嘗試做未經授權的事情時在其他路線上一樣),而是我從標題中得到錯誤

你能建議我能做什么嗎? 謝謝

exports.uploadImage = (req, res) => {
  const BusBoy = require("busboy");
  const path = require("path");
  const os = require("os");
  const fs = require("fs");

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

  let imageFileName;
  let imageToBeUploaded = {};

  busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
    if (mimetype !== "image/jpeg" && mimetype !== "image/png") {
      return res.status(400).json({ error: "Wrong file type submitted" });
    }
    
    const imageExtension = filename.split(".")[filename.split(".").length - 1];
    
    imageFileName = `${Math.round(
      Math.random() * 100000000000
    )}.${imageExtension}`;
    const filepath = path.join(os.tmpdir(), imageFileName);
    imageToBeUploaded = { filepath, mimetype };
    file.pipe(fs.createWriteStream(filepath));
  });
  busboy.on("finish", () => {
    admin
      .storage()
      .bucket()
      .upload(imageToBeUploaded.filepath, {
        resumable: false,
        metadata: {
          metadata: {
            contentType: imageToBeUploaded.mimetype,
          },
        },
      })
      .then(() => {
        const imageUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${imageFileName}?alt=media`;
        return db.doc(`/users/${req.user.handle}`).update({ imageUrl });
      })
      .then(() => {
        return res.json({ message: "Image uploaded successfully" });
      })
      .catch((err) => {
        console.error(err);
        return res.status(500).json({ error: err.code });
      });
  });
  busboy.end(req.rawBody);
};

來自郵遞員:///

SyntaxError: Unexpected token - in JSON at position 0

   at JSON.parse (<anonymous>)
   at createStrictSyntaxError (C:\Users\Marius\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\json.js:158:10)
   at parse (C:\Users\Marius\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\json.js:83:15)
   at C:\Users\Marius\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\read.js:121:18
   at invokeCallback (C:\Users\Marius\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:224:16)
   at done (C:\Users\Marius\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:213:7)
   at IncomingMessage.onEnd (C:\Users\Marius\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:273:7)
   at IncomingMessage.emit (events.js:327:22)
   at endReadableNT (_stream_readable.js:1221:12)
   at processTicksAndRejections (internal/process/task_queues.js:84:21)
///

我認為問題是當我嘗試發送請求(表單數據並在 Postman 上上傳圖片)時,因為它在其他路線上出現了同樣的問題,我預計會出現另一個錯誤或類似的問題))

感謝大家的時間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM