提问人:Gbenga B Ayannuga 提问时间:10/13/2023 最后编辑:marc_sGbenga B Ayannuga 更新时间:11/12/2023 访问量:30
无法使用 Express (multer) 将图像上传到 firebase
Unable to upload image to firebase using Express (multer)
问:
我正在使用 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");
}
}
}
答: 暂无答案
评论