The Getting started tutorial shows how to create a Netlify function and call it directly in the browser using netlify dev
. I wrote a function to query a Fauna database, which returns the correct data in the browser, but when I try to access that data in my application, I keep getting 404 not found. Here’s my function, which just gets the document from the ‘maps’ collection with a specific _id of ‘1’
const faunadb = require('faunadb')
const q = faunadb.query;
exports.handler = async () => {
// get a db client
const client = new faunadb.Client({
secret: process.env.FAUNA_KEY,
domain: 'db.us.fauna.com',
});
return await client.query(
q.Get(q.Ref(q.Collection('maps'),'1'))
)
.then((ret) => {
return {
statusCode: 200,
body: JSON.stringify(ret.data),
}
})
.catch((err) => {
console.log('Error:', err)
return {
statusCode: 400,
body: JSON.stringify(err)
}
})
};
If I run netlify dev and browse to http://localhost:8888/.netlify/functions/get-mapid
I get the expected response data {"mapId":12322}
So I wrote an api to access this data from my application. That’s where I’m having trouble.
read = async () => {
return await fetch('/.netlify/functions/get-mapid').then((response) => {
console.log('resp in api', response)
return response.json()
})
}
export default {
read: read,
}
When this read function gets called from a preact component, a 404 response is always logged. I’m sure I’m doing something dumb, but I’m having trouble seeing it. Thanks for any help on this!
Best,
Dow