0
0
YYusifHasanov
The code first imports the firebase and firestore modules. It then uses the storageRef object to store a PDF file. Next, an uploadTask object is created and initialized with a state_changed event handler. When the state_changed event occurs, the snapshot property is used to get a current representation of the PDF file. The createdAt and serverTimestamp fields are then set on the PDFRef object. Finally, the PDFRef object is stored in the firestore collection "pdfs.
import * as firebase from 'firebase/app';
import 'firebase/storage';
import 'firebase/firestore';
const pdf = new File(['content'], 'file.pdf', { type: 'application/pdf' });
const storageRef = firebase.storage().ref().child('files/file.pdf');
const uploadTask = storageRef.put(pdf);
uploadTask.on('state_changed', (snapshot) => {
// Handle progress
}, (error) => {
console.log(error);
}, () => {
storageRef.getDownloadURL().then((url) => {
const pdfRef = firebase.firestore().collection('pdfs').doc();
pdfRef.set({
url,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
});
});
});