Hi - I’ve come here after no success with Stack Overflow…
Hope it’s in the right section!
I’m attempting to render a component with props passed in as entries from my MongoDB database. To do this, I’m using Serverless Functions hosted on Vercel.
I can successfully post the entries to my database with no problem, however I can’t then render the entries in my dashboard. I get a timeout error after 10 seconds:
[GET] /api/fetchDebtCards
09:54:52:69
2020-10-13T08:55:02.792Z bad0eafa-7006-4749-886a-ba9842dd72a7 Task timed out after 10.01 seconds
I’ve tried playing around with the code, but just can’t figure out what I’m doing wrong.
Here’s my code -
My Schema being pulled:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const SubmitDebtSchema = new Schema ({
creditCard: String,
personalLoan: String,
provider: String,
balance: Number,
limit: Number,
monthly: Number,
interest: Number,
borrowed: Number
});
module.exports = mongoose.model('submitdebts', SubmitDebtSchema);
My ‘fetch’ function pulled from API -> fetchDebtCards.js :
const mongoose = require("mongoose");
const SubmitDebt = require("./submitDebtSchema");
require("dotenv").config();
mongoose.connect(process.env.MONGO_URI)
module.exports = async (req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
await SubmitDebt.find({});
};
My React front-end, attempting to pull and render the cards:
import React, { Component } from 'react';
import axios from 'axios';
import './individual-debts.css';
import IndividualDebtCard from '../individual-debts/individual-debt-card';
class IndividualDebts extends Component {
constructor(props) {
super(props)
this.state = {
debts: []
}
}
componentDidMount() {
axios.get("/api/fetchDebtCards")
.then(({
response, data }) => {
this.setState({
debts: response.data
})
.catch(e => {
console.log(e)
})
})
}
render() {
const fetchDebts = this.state.debts.map (debt => {
return (
<IndividualDebtCard key={debt._id}
provider={debt.provider}
type={debt.creditCard === 'true' ? debt.creditCard : "Personal Loan" }
balance={debt.balance}
limit={debt.creditCard === 'true' ? debt.limit : debt.borrowed}
monthly={debt.monthly}
interest={debt.interest} />
)
})
return (
<div>
<section className="individual-debts-section">
<div className="individual-debts-container">
<div className="individual-debts-heading">
<h3>Individual Breakdown</h3>
</div>
<div className="individual-debts-card-container">
{ fetchDebts }
</div>
I feel like it’s something really obvious I’m doing wrong. I’ve been scratching my head about this for a couple of days now. I don’t know if it’s perhaps related to it being a serverless function, or possibly the fact I’m importing the component and attempting to use the data as props? I really can’t figure it out
Any help would be really really really appreciated. Thank you.