-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reutilização de função de find e encadeamento de promises
- Loading branch information
1 parent
324281a
commit f0d24e4
Showing
1 changed file
with
20 additions
and
40 deletions.
There are no files selected for viewing
60 changes: 20 additions & 40 deletions
60
website/src/services/FindOneByCollection/FindOneByCollection.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,25 @@ | ||
import { React, Component } from "react"; | ||
import { db, databaseRef, get } from "../../init-firebase"; | ||
import FindAllByCollection from "../../services/FindAllByCollection/FindAllByCollection"; | ||
|
||
const dbRef = databaseRef(db); | ||
|
||
//função async ou promise | ||
// Promise: FindOneByCollection('elementsCollection').then(elementsArray) | ||
// async: const elementsArray = await FindOneByCollection('elementsCollection') | ||
function FindOneByCollection(collection) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
get(dbRef, collection).then((response) => { | ||
if (response.exists()) { | ||
let elements = response.val()[collection]; | ||
let elementsArray = []; | ||
|
||
if ( | ||
elements && | ||
elements !== undefined && | ||
elements !== null | ||
) { | ||
if (typeof elements === "object") { | ||
//loop para objeto | ||
elementsArray = Object.keys(elements).map( | ||
(key) => elements[key].form | ||
); | ||
} | ||
//console.log("Got data ", collection, elementsArray); | ||
|
||
const lastElement = | ||
elementsArray[elementsArray.length - 1]; | ||
resolve(lastElement); | ||
} | ||
} else { | ||
//console.log("No data available ", collection); | ||
resolve(null); | ||
} | ||
}); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
return FindAllByCollection(collection) | ||
.then(elements => { | ||
return elements[elements.length - 1] | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
// throw new Error(); | ||
return null; | ||
}); | ||
} | ||
|
||
// Async: FindLastOneByCollection | ||
// async function FindLastOneByCollection(collection) { | ||
// try { | ||
// const elements = await FindAllByCollection(collection) | ||
// return elements[elements.length - 1]; | ||
// } catch(err) { | ||
// return null; | ||
// } | ||
// } | ||
|
||
export default FindOneByCollection; |