// const admin = require('firebase-admin'); const express = require('express'); // const db = admin.database(); const router = express.Router(); // router.use(bodyParser.json()); // API router.post('/words', (req, res) => { const {userId, word} = req.body; db.ref(`words/${userId}`).push({word}); res.sendStatus(201); });
userId
) and the word ( word
), then save the word in the collection of words
. // API router.post('/words', (req, res) => { const {userId, word} = req.body; db.ref(`words/${userId}`).push({word}, error => { if (error) { res.sendStatus(500); // , , Sentry } else { res.sendStatus(201); } }; });
// API router.get('/words', (req, res) => { const {userId} = req.query; db.ref(`words/${userId}`).once('value') .then( snapshot => { res.send(snapshot.val()); }); });
async/await
: // API router.get('/words', async (req, res) => { const {userId} = req.query; const wordsSnapshot = await db.ref(`words/${userId}`).once('value'); res.send(wordsSnapshot.val()) });
async
, added before the parameters (res, req)
arrow function, and to the await
keyword, which precedes the expression db.ref()
.db.ref()
method returns a promise. This means that we can use await
to “suspend” the execution of the script. The await
can be used with any promises.res.send()
method, located at the end of the function, will only be called after the db.ref()
promise db.ref()
.async/await
, in cases where you need to chain several asynchronous requests into a chain. const example = require('example-library'); example.firstAsyncRequest() .then( fistResponse => { example.secondAsyncRequest(fistResponse) .then( secondResponse => { example.thirdAsyncRequest(secondResponse) .then( thirdAsyncResponse => { // }); }); });
async/await
: const example = require('example-library'); const runDemo = async () => { const fistResponse = await example.firstAsyncRequest(); const secondResponse = await example.secondAsyncRequest(fistResponse); const thirdAsyncRequest = await example.thirdAsyncRequest(secondResponse); }; runDemo();
await
keyword can be wrapped into a single try/catch
to handle any errors: const example = require('example-library'); const runDemo = async () => { try { const fistResponse = await example.firstAsyncRequest(); const secondResponse = await example.secondAsyncRequest(fistResponse); const thirdAsyncRequest = await example.thirdAsyncRequest(secondResponse); } catch (error) { // } }; runDemo();
async/await
.Promise.all()
method to execute queries in parallel: // API router.get('/words', async (req, res) => { const wordsRef = db.ref(`words`).once('value'); const usersRef = db.ref(`users`).once('value'); const values = await Promise.all([wordsRef, usersRef]); const wordsVal = values[0].val(); const userVal = values[1].val(); res.sendStatus(200); });
snapshot.val()
. This can cause problems with parsing JSON on the client. fetch('https://your-domain.com/api/words') .then( response => response.json()) .then( json => { // }) .catch( error => { // });
snapshot.val()
returned by Firebase can be either a JSON object or a null
value if no records were found. If you return null
, then json.response()
in the above code will json.response()
error, as it will try to null
, which is not an object, to parse.Object.assign()
to always return the object to the client: // API router.get('/words', async (req, res) => { const {userId} = req.query; const wordsSnapshot = await db.ref(`words/${userId}`).once('value'); // res.send(wordsSnapshot.val()) // const response = Object.assign({}, snapshot.val()); res.send(response); });
Source: https://habr.com/ru/post/332768/
All Articles