Skip to content

Instantly share code, notes, and snippets.

@waldothedeveloper
Last active March 12, 2022 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waldothedeveloper/b3e8f00180c0a6f74eb7318d2dbf0a1e to your computer and use it in GitHub Desktop.
Save waldothedeveloper/b3e8f00180c0a6f74eb7318d2dbf0a1e to your computer and use it in GitHub Desktop.
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;
}
}
@waldothedeveloper
Copy link
Author

Line 21 where I'm implemeting .sort() is not sorting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment