Skip to content

Commit

Permalink
un-gitignore the netlify functions folder
Browse files Browse the repository at this point in the history
  • Loading branch information
groupanimal committed Feb 12, 2020
1 parent 080e802 commit 5ff0774
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ yarn-error.log*

# Local Netlify folders
.netlify
functions

# Logs
logs
Expand Down
57 changes: 57 additions & 0 deletions functions/get-questions/get-questions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// import Airtable.js package
// import Airtable from 'airtable'; // produces a SyntaxError ('cannot use import statement outside a module')
const Airtable = require('airtable');

// import local environment variables if in dev environment - is this necessary?
// import dotenv from 'dotenv';
// if (process.env.NODE_ENV === 'development') {
// dotenv.config()
// { AIRTABLE_API_URL, AIRTABLE_API_KEY, AIRTABLE_BASE_ID } = process.env;
// }

// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method
// the handler function can take event and context params (not required in this case)
// it can also take a callback as third argument, but we will stick to promises
exports.handler = async () => {
// grab airtable variables from Netlify environment (uploaded via UI)
const { AIRTABLE_API_KEY, AIRTABLE_BASE_ID, AIRTABLE_API_URL } = process.env;

// configure connection to airtable base
Airtable.configure({
endpointUrl: AIRTABLE_API_URL,
apiKey: AIRTABLE_API_KEY,
});
const base = Airtable.base(AIRTABLE_BASE_ID);

let data = [];

// select all records - only need to check first page since questions won't exceed 100 entries
// need an await to ensure data is assigned to records before returning response
await base('UserQuestions')
.select({
maxRecords: 100,
view: 'Grid view',
})
.firstPage()
.then(records => {
records.forEach(record => {
data.push(record.fields);
});
})
.catch(err => {
console.log(err.status);
});

// finally return any data collected as body of a response object
try {
return {
statusCode: 200,
body: JSON.stringify(data),
headers: {
'content-type': 'application/json',
},
};
} catch (err) {
return { statusCode: 500, body: err.toString() };
}
};
2 changes: 2 additions & 0 deletions src/App/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { getQuestions } from '../utils/getData';
function App() {
const [questions, setQuestions] = useState(null);

console.log(process.env.NODE_ENV);

useEffect(() => {
getQuestions().then(records => {
console.log(records);
Expand Down

0 comments on commit 5ff0774

Please sign in to comment.