Hello! Really excited to get some help here.
I’ve deployed a netlify function that successfully runs on my local server. However, once deployed, I’ve started receiving a 502 gateway error pretty much instantly (i don’t think its timing out).
The way my code works is that my app checks for certain parameters in the url. The url that I have been practicing with is https://charming-fox-819842.netlify.app/?tagId=557&eCode=777&enc=888&cmac=999 . These parameters are sent to netlify via a POST request. Then, netlify parses the request and checks Airtable to see if these values exist within my database. These values do exist in my database and when ran locally will return the desired information. What I’ve found based on the function log is that the “await table.select()…” function is not being called or at least breaks down at that point.
Here is a snip of my netlify code:
const fetch = require("node-fetch")
const Airtable = require("airtable")
const handler = async (event , context, callback) => {
// Configure Airtable base connection
const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(
process.env.AIRTABLE_BASE_ID
)
// Configure table name
const table = base(process.env.AIRTABLE_TABLE_NAME)
// this is where the function starts, the url paramters are parsed and saved as fields
try {
const { httpMethod } = event
let fields = JSON.parse(event.body)
const { tagId, eCode, enc, cmac } = fields
console.log(fields)
// the fields are posted correctly in the netlify function log
// the date the function was ran is saved as an ISOString
const triggeredDate = new Date()
console.log(triggeredDate)
const triggeredTimestamp = triggeredDate.toISOString()
// the triggered date is posted correctly in the netlify function log
// Only allow POST methods
if (httpMethod !== "POST") {
return {
statusCode: 405,
body: JSON.stringify({ message: "Method Not Allowed" })
}
} else if (!tagId || !eCode || !enc || !cmac) {
return {
statusCode: 400,
body: JSON.stringify({ message: "Bad Request" })
}
}
console.log("made it here netlify")
// this console log is posted correctly in the netlify function log
// each url parameter is saved as its own constant to be checked against Airtable
const fieldTagId = 'tagId'
const fieldECode = 'eCode'
const fieldENC = 'enc'
const fieldCMAC = 'cmac'
// this function checks any record/row in Airtable has exactly all four values
await table
.select({
filterByFormula: `AND({${fieldTagId}} = ${tagId}, {${fieldECode}} = ${eCode}, {${fieldENC}} = ${enc}, {${fieldCMAC}} = ${cmac})`,
})
.all(function(err, records) {
if (err) {
console.error(err);
return;
}
// this console command does not appear and I believe this is where the code is breaking down
console.log("interesting")
// if there is a record with all four values, the timestamp of when the function was triggered is compared to the timestamp within Airtable
if (records && records.length >= 1) {
records.forEach(function(record) {
const airtableTimestamp = record.get('createdTime')
console.log('Retrieved', airtableTimestamp);
const d1 = new Date(airtableTimestamp);
const d2 = new Date(triggeredTimestamp);
const timeDif = d2 - d1
console.log(timeDif)
// if the time difference is greater than 30 minutes, then the "Rune Decayed" response is returned to App.js
if ((timeDif) > 30 * 60 * 1000) {
console.log("old rune")
return {
statusCode: 202,
body: JSON.stringify({ message: "Rune Decayed"})
}
// if the time difference is less than 30 minutes, then the "Authentic" response is returned to App.js
} else {
return {
statusCode: 202,
body: JSON.stringify({ message: "Authentic" })
}
}
});
// if there is not a record within Airtable that has all exactly all four paramters, then these parameters are sent to the ETRNL API - etrnlAuth() -- IGNORE THIS, NOT RELEVANT TO CURRENT ISSUE
} else {
console.log("No records of paramters found")
console.log("Sending paramters to ETRNL API for authentication")
etrnlAuth(tagId, eCode, enc , cmac, base, table);
}
});
} catch (error) {
console.log(error)
return {
statusCode: 500,
body: "Oops! Something went wrong." }
}
}
Here is a snip of the error from my console log:
the rune-auth.js function seems to last only 680 ms?
My code operates normally on my local machine and will return the records information I request after the “await table.select()” function runs. I’m beginning to think its the await aspect not being triggered? Any help would be great! Please let me know if you need more information.