0

I'm doing project by a video, but the content seams to be out of date, because i've been runing into a lot of problems that the guy isn't. I'm fairly new to node js and firebase in fact i'm learning along the way.

My problem is that when I enter this function on() of bus boy my variable simply gets Undefined. I can't understand why. that leads to the URL storage in DB being :
"https://firebasestorage.googleapis.com/v0/b/socialape-f5c4d.appspot.com/o/undefined?alt=media"

here is my code

const { db, admin } = require("../util/admin");

const config = require("../util/config");

const firebase = require("firebase");
firebase.initializeApp(config);

const {
  validateSingnupData,
  validateLoginData,
} = require("../util/validators");

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

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

  let imageFileName; // here it's been defined
  let imageToBeUploaded = {};

  busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
    console.log(fieldname);
    console.log(filename);
    console.log(mimetype);

    // image.png
    const imageExtention = filename.split(".")[filename.split(".").length - 1];
    //random imageFileName 2641654465.png for example
    const imageFileName = `${Math.round(
      Math.random() * 100000000000
    )}.${imageExtention}`;

    const filepath = path.join(os.tmpdir(), imageFileName);
    imageToBeUploaded = { filepath, mimetype };
    file.pipe(fs.createWriteStream(filepath));
  });

  console.log("imageFileName 1:" + imageFileName);
  busboy.on("finish", () => {
    console.log("imageFileName 2:" + imageFileName);
    admin
      .storage()
      .bucket(config.storageBucket)
      .upload(imageToBeUploaded.filepath, {
        resumable: false,
        metadata: {
          metadata: {
            contentType: imageToBeUploaded.mimetype,
          },
        },
      })
      .then(() => {
        console.log("imageFileName 3:" + imageFileName);
        const imageUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${imageFileName}?alt=media`;
        console.log(imageUrl);
        return db.doc(`/users/${request.user.handle}`).update({ imageUrl });
      })
      .then(() => {
        return response.json({ message: "iamge uploaded succesfuly" });
      })
      .catch((err) => {
        console.error(err);
        return response.status(500).json({ error: err.code });
      });
  });
  busboy.end(request.rawBody);
};

this is what i get in the console

Google API requested!
   - URL: "https://www.googleapis.com/oauth2/v4/token"
   - Be careful, this may be a production service.
>  imageFileName 1:undefined
>  image
>  17124052466014.jpg
>  image/jpeg
>  imageFileName 2:undefined
!  Google API requested!
   - URL: "https://storage.googleapis.com/upload/storage/v1/b/socialape-f5c4d.appspot.com/o?uploadType=multipart&name=91288441121.jpg"
   - Be careful, this may be a production service.
>  imageFileName 3:undefined
>  https://firebasestorage.googleapis.com/v0/b/socialape-f5c4d.appspot.com/o/undefined?alt=media
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

2 Answers2

0

Your missing the most important thing, pipe the request with busboy:

request.pipe(busboy)
Talg123
  • 1,468
  • 1
  • 10
  • 15
0

I had a similar issue but fixed mined by installing a lower version of busboy used as at the time of the tutorial. In my case, I installed version 0.3.0 instead of the latest version by running

npm I busboy@0.3.0,