无法使用 Express (multer) 将图像上传到 firebase

Unable to upload image to firebase using Express (multer)

提问人:Gbenga B Ayannuga 提问时间:10/13/2023 最后编辑:marc_sGbenga B Ayannuga 更新时间:11/12/2023 访问量:30

问:

我正在使用 Firebase 作为后端构建自己的后端,我希望能够上传图像,但我一直收到此错误:

TypeError:无法读取未定义的属性(读取“mimetype”)

即使我删除了一个属性,我仍然会得到另一个属性错误。

这是我的代码:

索引.ts

import * as multer from "multer";

// dotenv.config();
const app = express();
initializeApp(firebaseConfig);
// admin.initializeApp(functions.config().functions);
admin.firestore();

export const openai = new OpenAIApi(configuration);
export const upload = multer({storage: multer.memoryStorage()});

app.use(bodyParser.json());
app.use(express.urlencoded({extended: false}));
app.use(cors({origin: true}));

routesConfig(app);
export const api = functions.https.onRequest(app);

路线

export function routesConfig(app: Application) {
  app.get("/", (req, res) => res.status(200).send("Welcome home!!!"));
  // registrations api---------------------------------------------------

  app.post("/verifyuser", upload.single("name"), verifyUserAccount);

}

verifyUserAccount 功能

export async function
verifyUserAccount(req: Request, res: Response): Promise<Response<unknown>> {
  try {
    const dd = await Uploading.uploadData( req.body.files, "gbenga", "gbenga");
    // const data = dataAuthService;
    return res.status(200).send({message: "Succesfull", dd});
  } catch (error) {
    logger.error("error is coming from wher" + error);
    return res.status(400).send(error);
  }
}

Uploading.uploadData 类

import { getStorage, ref, uploadBytesResumable } from "firebase/storage";
import { logger } from "firebase-functions/v1";
import { injectable } from "inversify";

@injectable()
export class Uploading {
   static async uploadData(file:any, userId:string, postId:string): Promise<string> {
    const bucket = getStorage();
    // const bucketDomain = "gs://uneleap-9b858.appspot.com";
    try {
      const dateTime = Date.now();
      const fileName = `${postId}/${userId}/${dateTime}`;
      const storageRef = ref(bucket, fileName);
      const metadata = {
          contentType: file.mimetype,
      };
      await uploadBytesResumable(storageRef, file.buffer, metadata);
      return fileName;
    //     // const subPaths = file.fieldname.split("/");
    //     // const fileName = subPaths[subPaths.length - 1];
    //     const destination = `${postId}/${userId}/${postId}`;
    //     await bucket.bucket().upload(file.path, { destination, resumable: true });
    //     const uploadedUrl = `https://${bucketDomain}/${destination}`; // Construct the URL\
    //     const uploadedImageLinks = await Promise.all(uploadedUrl);
    // return uploadedImageLinks;
    } catch (error) {
      logger.error("Error =>", error);
      throw new Error("this is from error");
    }
  }

}
javascript 打字稿 firebase-storage firebase-admin

评论

0赞 Doug Stevenson 10/13/2023
哪一行代码导致了错误?请编辑问题以分享到目前为止的调试结果。
0赞 Gbenga B Ayannuga 10/13/2023
@DougStevenson Uploading.uploadData 类
0赞 Doug Stevenson 10/13/2023
具体是哪条线?
0赞 Gbenga B Ayannuga 10/13/2023
我现在尝试检查文件是否为 null,现在说为 null

答: 暂无答案