I created a new React site, mellow-lolly-ecaae8
, to learn how to use the PlanetScale integration and also to make my first simple serverless function on Netlify.
I created an account on Planetscale and used it to set up my database, wouldattend-2023
. I used Planetscale’s online console to make the schema and insert some rows, which succeeded.
(By the way, why is Planetscale offering me a DATABASE_URL=mysql://
string? I would expect to need this in a .env file, but the Netlify Planetscale documentation doesn’t mention it. Does withPlanetscale
just handle it on Netlify behind the scenes?)
My package.json
:
"dependencies": {
"@netlify/planetscale": "^1.0.0",
"@planetscale/database": "^1.6.0",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^13.2.1",
"formdata-polyfill": "^4.0.10",
"mysql2": "^3.2.0",
"netlify-identity-widget": "^1.9.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.9",
"web-vitals": "^2.1.0"
},
Here’s my serverless function at functions/getProposals.js
:
import { Handler } from "@netlify/functions";
import { withPlanetscale } from "@netlify/planetscale";
export const handler: Handler = withPlanetscale(async (event, context) => {
const {
planetscale: { connection },
} = context;
const { body } = event;
if (!body) {
return {
statusCode: 400,
body: "Missing body",
};
}
const proposals = await connection.execute(
"SELECT title, description FROM Sessions)"
);
return {
statusCode: 200,
body: JSON.stringify(proposals)
};
});
Here’s the component ProposalsListComponent.js
where I call it with a fetch:
import React, { useState, useEffect } from 'react';
const ProposalList = () => {
const [proposals, setProposals] = useState([]);
useEffect(() => {
async function fetchData() {
try {
const response = await fetch('/functions/getProposals', { method: 'GET' });
console.log("getProposals/response ", response)
setProposals(response.data);
} catch (error) {
console.error('error: ', error);
const errorMessage = await error.text();
console.log('errorMessage ', errorMessage);
}
}
fetchData();
}, []);
return (
<div>
{proposals?.map((proposal) => (
<div key={proposal.id}>
<h3>{proposal.title}</h3>
<p>{proposal.description}</p>
</div>
))}
</div>
);
};
export default ProposalList;
When I run it locally with netlify dev
, it hits "http://localhost:8888/.netlify/functions/getProposals"
and returns a 404.
When I commit and deploy this on Netlify, the console.log message in the deploy shows
Failed to load resource: the server responded with a status of 400 ()
The Netlify Functions log just says:
Mar 26, 11:17:18 AM: 3b00941e Duration: 4.42 ms Memory Usage: 62 MB Init Duration: 193.92 ms
Why would this be?