Last active
March 12, 2022 23:48
-
-
Save waldothedeveloper/b3e8f00180c0a6f74eb7318d2dbf0a1e to your computer and use it in GitHub Desktop.
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
import { Headers } from "node-fetch"; | |
import { classifyTaks } from "../../utils/classifyTaks"; | |
import fetch from "node-fetch"; | |
export default async function fetchTasks(req, res) { | |
const url = `http://localhost:3000/api/${req.params.id}`; | |
// ! IMPORTANT: A .env file should be used here for the api-key value, but for the sake of this example, I'm using a hardcoded value. | |
const headers = new Headers(); | |
headers.append("api-key", "globus"); | |
const requestOptions = { | |
method: "GET", | |
headers: headers, | |
redirect: "follow", | |
}; | |
try { | |
const result = await fetch(url, requestOptions).then((res) => res.json()); | |
const classifiedTasks = await classifyTaks(result.DATA); | |
const sorted = await classifiedTasks.sort(function (a, b) { | |
if (a.secondaryId === 0 || b.secondaryId === 1 || a.secondaryId === 1 || b.secondaryId === 0) { | |
return a.secondaryId - b.secondaryId; | |
} else { | |
const dateA = new Date(a.end_date); | |
const dateB = new Date(b.end_date); | |
return dateA - dateB; | |
} | |
}); | |
console.log("sorted: ", sorted); | |
res.status(200).json(classifiedTasks); | |
} catch (err) { | |
const error = new Error("An error ocurred while fetching the data"); | |
error.status = res.status; | |
error.info = await res.json(); | |
throw error; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 21 where I'm implemeting .sort() is not sorting.