I am working on an app that creates a payment schedule using the Toolbox plugin. The schedule includes the month, principal payment, interest payment, and balance.
I’m logging the schedule to the console, and everything looks good, but I cannot figure out how to return it to the JS2B plugin and get it to display in a repeating group.
function generatePaymentSchedule(loanAmount, annualRate, interestOnlyPeriod, amortizationPeriod) {
let monthlyRate = (annualRate / 100) / 12;
let totalMonths = interestOnlyPeriod + amortizationPeriod;
let paymentSchedule = [];
// Interest-only payments
let interestPayment = loanAmount * monthlyRate;
for (let month = 1; month <= interestOnlyPeriod; month++) {
let payment = {
month: month,
payment: interestPayment.toFixed(2),
principal: 0,
interest: interestPayment.toFixed(2),
balance: loanAmount.toFixed(2)
};
paymentSchedule.push(payment);
}
// Amortized payments
let amortizedPayment = (loanAmount * monthlyRate) / (1 - Math.pow(1 + monthlyRate, -amortizationPeriod));
let remainingBalance = loanAmount;
for (let month = interestOnlyPeriod + 1; month <= totalMonths; month++) {
let interestPortion = remainingBalance * monthlyRate;
let principalPortion = amortizedPayment - interestPortion;
remainingBalance -= principalPortion;
let payment = {
month: month,
payment: amortizedPayment.toFixed(2),
principal: principalPortion.toFixed(2),
interest: interestPortion.toFixed(2),
balance: remainingBalance.toFixed(2)
};
paymentSchedule.push(payment);
}
// Log the payment schedule to the console for checking
console.log("Payment Schedule: ", paymentSchedule);
// Return the paymentSchedule as a list to outputlist1
bubble_fn_schedule({
outputlist1: paymentSchedule
});
}
// Retrieve user inputs and call the function
let loanAmount = loanAmount;
let annualRate = annualRate;
let interestOnlyPeriod = interestOnlyPeriod;
let amortizationPeriod = amortizationPeriod;
generatePaymentSchedule(loanAmount, annualRate, interestOnlyPeriod, amortizationPeriod);
This is the JavaScript. Everything logs correctly to the console.
What type did you set for outputlist1
in the JS2B element?
Try out the approach mentioned in this thread - I'm having trouble processing data in Bubble.io with JavaScript! 🤯 - #2 by adamhholmes
Although you can work with Custom JavaScript in Bubble (in various ways, including the toolbox plugin), there’s literally no way you can access or use the output data (unless it’s primitive datatypes, or existing Bubble objects) in the Bubble editor…
Except, that is, by outputting that data as a JSON string, and then sending that string, via the API connector, to a backend workflow in your app, only to return it to the page where it was created, whereby it can then be parsed (thanks to the API Connector) into useable data (API data), that can then be accessed in the editor (ridiculous… yes).
@shankar Thanks for sending this.
I created a Data Type called Payments to match the output of the JavaScript and I’m trying to send that to the plugin.