how can I use fetched data from firebase to create a pdf document using javascript, nodejs and pdfkit?

I’m working on automating the generation and download of loan agreements as PDF documents. The data for these agreements comes from two parties, “borrower” and “lender,” stored in a Firebase Realtime Database. My Firebase integration is working smoothly, fetching and displaying the stored data as intended. However, I’m facing some challenges in utilizing this data to create a PDF document using Node.js and pdfkit.

Here’s the setup: I have two JavaScript files to manage different aspects of the process. One handles the logic for fetching the data and other related operations, while the other is responsible for creating the PDF file using the fetched data.

However, I’ve encountered an issue where the terminal throws an error, stating that “Export ‘firebase’ is not defined in module,” even though Firebase is defined and imported correctly in the root file.

Here’s the relevant part of my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// myapprovedloan-details.js
// Function to fetch loan data for a specific user and loan
export function fetchLoanData(userID, loanID) {
// Ensure that both userID and loanID are valid strings
if (!userID || !loanID) {
console.error('Invalid userID or loanID.');
return;
}
// Construct the database reference path dynamically
const loanDataRef = firebase.database().ref(`myApprovedLoans/${userID}/${loanID}/loanData`);
// Fetch loan data from the database
loanDataRef.once('value', (snapshot) => {
const loanData = snapshot.val();
if (loanData) {
// Process loan data as needed
console.log('Loan data:', loanData);
// Call functions to update UI with loan data
// Display loan information
// Update other UI elements with loan data
} else {
console.error('No loan data found for loan ID:', loanID);
// Handle the case where no loan data is found
}
});
// Export Firebase
export { firebase };
}
</code>
<code>// myapprovedloan-details.js // Function to fetch loan data for a specific user and loan export function fetchLoanData(userID, loanID) { // Ensure that both userID and loanID are valid strings if (!userID || !loanID) { console.error('Invalid userID or loanID.'); return; } // Construct the database reference path dynamically const loanDataRef = firebase.database().ref(`myApprovedLoans/${userID}/${loanID}/loanData`); // Fetch loan data from the database loanDataRef.once('value', (snapshot) => { const loanData = snapshot.val(); if (loanData) { // Process loan data as needed console.log('Loan data:', loanData); // Call functions to update UI with loan data // Display loan information // Update other UI elements with loan data } else { console.error('No loan data found for loan ID:', loanID); // Handle the case where no loan data is found } }); // Export Firebase export { firebase }; } </code>
// myapprovedloan-details.js

// Function to fetch loan data for a specific user and loan
export function fetchLoanData(userID, loanID) {
    // Ensure that both userID and loanID are valid strings
    if (!userID || !loanID) {
        console.error('Invalid userID or loanID.');
        return;
    }

    // Construct the database reference path dynamically
    const loanDataRef = firebase.database().ref(`myApprovedLoans/${userID}/${loanID}/loanData`);

    // Fetch loan data from the database
    loanDataRef.once('value', (snapshot) => {
        const loanData = snapshot.val();
        if (loanData) {
            // Process loan data as needed
            console.log('Loan data:', loanData);
            // Call functions to update UI with loan data
            // Display loan information
            // Update other UI elements with loan data
        } else {
            console.error('No loan data found for loan ID:', loanID);
            // Handle the case where no loan data is found
        }
    });
// Export Firebase
export { firebase };
}

i’m fetching the lender’s data from the logged-in user.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> // Get the currently logged-in user
const user = firebase.auth().currentUser;
if (user) {
const lenderID = user.uid; // Assuming user IDs are stored as UIDs in Firebase Authentication
const userDataRef = firebase.database().ref(`myApprovedLoans/${userID}`);
userDataRef.once('value', (snapshot) => {
const userData = snapshot.val();
if (userData) {
// Start the lending flow
showModal('startModal'); // Show the start modal
// Pass the lenderID to moveLoanToMyApprovedLoans
moveLoanToMyApprovedLoans(userData, lenderID);
}
});
} else {
console.log('No user logged in.');
}
}
</code>
<code> // Get the currently logged-in user const user = firebase.auth().currentUser; if (user) { const lenderID = user.uid; // Assuming user IDs are stored as UIDs in Firebase Authentication const userDataRef = firebase.database().ref(`myApprovedLoans/${userID}`); userDataRef.once('value', (snapshot) => { const userData = snapshot.val(); if (userData) { // Start the lending flow showModal('startModal'); // Show the start modal // Pass the lenderID to moveLoanToMyApprovedLoans moveLoanToMyApprovedLoans(userData, lenderID); } }); } else { console.log('No user logged in.'); } } </code>
    // Get the currently logged-in user
    const user = firebase.auth().currentUser;
    if (user) {
        const lenderID = user.uid; // Assuming user IDs are stored as UIDs in Firebase Authentication
        const userDataRef = firebase.database().ref(`myApprovedLoans/${userID}`);
        userDataRef.once('value', (snapshot) => {
            const userData = snapshot.val();
            if (userData) {
                // Start the lending flow
                showModal('startModal'); // Show the start modal
                // Pass the lenderID to moveLoanToMyApprovedLoans
                moveLoanToMyApprovedLoans(userData, lenderID);
            }
        });
    } else {
        console.log('No user logged in.');
    }
}

and the second code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// test.js
import pdfkit from 'pdfkit'; // Import the entire module
import fs from 'fs';
import { fetchLoanData } from './myapprovedloan-details.js';
// Generate a PDF document from the fetched loan data
const generatePDF = (loanData) => {
// Create a new PDF document
const doc = new pdfkit();
// Pipe the PDF document to a file
doc.pipe(fs.createWriteStream('loan-data.pdf'));
// Draw the data on the PDF document
doc.fontSize(25).text(loanData.name, 100, 100);
// End the PDF document
doc.end();
};
// Call fetchData function from myapprovedloan-details.js to start fetching loan data
fetchLoanData()
.then((loanData) => {
if (loanData) {
generatePDF(loanData);
}
})
.catch((error) => {
// Handle the error
console.error('Error fetching loan data:', error);
});
</code>
<code>// test.js import pdfkit from 'pdfkit'; // Import the entire module import fs from 'fs'; import { fetchLoanData } from './myapprovedloan-details.js'; // Generate a PDF document from the fetched loan data const generatePDF = (loanData) => { // Create a new PDF document const doc = new pdfkit(); // Pipe the PDF document to a file doc.pipe(fs.createWriteStream('loan-data.pdf')); // Draw the data on the PDF document doc.fontSize(25).text(loanData.name, 100, 100); // End the PDF document doc.end(); }; // Call fetchData function from myapprovedloan-details.js to start fetching loan data fetchLoanData() .then((loanData) => { if (loanData) { generatePDF(loanData); } }) .catch((error) => { // Handle the error console.error('Error fetching loan data:', error); }); </code>
// test.js

import pdfkit from 'pdfkit'; // Import the entire module
import fs from 'fs';
import { fetchLoanData } from './myapprovedloan-details.js';

// Generate a PDF document from the fetched loan data
const generatePDF = (loanData) => {
    // Create a new PDF document
    const doc = new pdfkit();

    // Pipe the PDF document to a file
    doc.pipe(fs.createWriteStream('loan-data.pdf'));

    // Draw the data on the PDF document
    doc.fontSize(25).text(loanData.name, 100, 100);

    // End the PDF document
    doc.end();
};

// Call fetchData function from myapprovedloan-details.js to start fetching loan data
fetchLoanData()
    .then((loanData) => {
        if (loanData) {
            generatePDF(loanData);
        }
    })
    .catch((error) => {
        // Handle the error
        console.error('Error fetching loan data:', error);
    });

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật