You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// console.log("NOT FOUND ROUTE req =>",req);// console.log("NOT FOUND ROUTE req.body.search =>",req._parsedOriginalUrl.search);// console.log("NOT FOUND ROUTE req.body.query =>",req._parsedOriginalUrl.query);// console.log("NOT FOUND ROUTE req.body.pathname =>",req._parsedOriginalUrl.pathname);// console.log("NOT FOUND ROUTE req.body._raw =>",req._parsedOriginalUrl._raw);// console.log("NOT FOUND ROUTE req.socket.server._handle =>",res.socket.server._handle);// console.log("NOT FOUND ROUTE req.socket._connectionKey =>",res.socket._connectionKey);// console.log("NOT FOUND ROUTE req.socket.incoming.orginalMethod =>",res.socket.orginalMethod);// console.log("NOT FOUND ROUTE req.socket.incoming._parsedOriginalUrl =>",res.socket._parsedOriginalUrl);// console.log("NOT FOUND ROUTE req.socket.incoming.route =>",res.socket.route);// console.log("NOT FOUND ROUTE req.socket._httpMessage =>",res.socket._httpMessage);```Javascriptconsole.log("NOT FOUND ROUTE req.body =>",req.body);console.log("NOT FOUND ROUTE req.route =>",req.route);console.log("NOT FOUND ROUTE req.socket =>",res);letsearch=req._parsedOriginalUrl.searchconsole.log("NOT FOUND ROUTE req.body search =>",search);letquery=req._parsedOriginalUrl.queryconsole.log("NOT FOUND ROUTE req.body query =>",query);letpathName=req._parsedOriginalUrl.pathnameconsole.log("NOT FOUND ROUTE req.body pathname =>",pathName);letraw=req._parsedOriginalUrl._rawconsole.log("NOT FOUND ROUTE req.body raw =>",raw);letserver=res.serverconsole.log("NOT FOUND ROUTE req.body server =>",server);letparser=res.parserconsole.log("NOT FOUND ROUTE req.body parser =>",parser);letreqRes=res.reqconsole.log("NOT FOUND ROUTE req.body reqRes =>",reqRes);letrawHeader=res.rawHeadersconsole.log("NOT FOUND ROUTE req.body rawHeader =>",rawHeader);letmethods=res.method// console.log("NOT FOUND ROUTE req.body methods =>", methods);// const { rawHeaders, method } = res.req// console.log("rawHeaders logged ==>", rawHeaders);// console.log("methods logged ==>", method);// const requestObject = [// {// reqSearch: search// },// {// reqQuery: query// },// {// pathname: pathName// },// {// reqRaw: raw// },// {// reqRoute: route// },// {// resServer: server// },// {// resParser: parser// },// {// resReq: reqRes// },// {// resRaw: rawHeader// },// {// resMethod: methods// }// ]// const requestBody = JSON.stringify(requestObject)// console.log(requestBody);// if (requestBody === "") {// console.log("requestBody is empty");// } else {// try {// fs.appendFile("requestBody.txt", requestBody, (err) => {// if (err) throw New(err)// console.log("File has been saved");// })// } catch (error) {// console.log(error);// }// }
body-parser inteprets the incoming HTTP request body and makes it available as key-value pairs in the body property of the request, req, parameter
method-override allows one to specify a custom function to be called during the middleware execution. In this custom function we will inspect the request body, req.body, for the presence of the desired token, _method in this case, and return its value to the middleware that will in turn override the request method
varbodyParser=require('body-parser');app.use(bodyParser.urlencoded({extended: false}));app.use(methodOverride(function(req,res){if(req.body&&typeofreq.body==='object'&&'_method'inreq.body){// look in urlencoded POST bodies and delete itvarmethod=req.body._method;deletereq.body._method;returnmethod;}}));
// export const homepage = (req, res) => {// const testRand = Math.floor(Math.random() * 100) + 1// console.log(testRand);// try {// res.render("index.ejs", { joker: "Hit the different routes we have to get your jokes... ", testRand })// } catch (error) {// console.error(error.message);// }// }// /** GET Methods */// /**// * @openapi// * '/jokeapi/':// * get:// * tags:// * - Homepage Controller// * summary: Display the homepage// * responses:// * 200:// * description: Fetched Successfully// * 400:// * description: Bad Request// * 404:// * description: Not Found// * 500:// * description: Server Error// */// app.get("/jokeapi/docs", homepage)
Checking if an item in an array of objects using .includes()
But before that can happen, you first have to extract that item using the .map() before you can apply the .includes()
Example below extracting Id from the array of objects
letusers=[{id: 1,name: "Alice",firstname: "Alicia"},{id: 2,name: "Bob",firstname: "Robert"},{id: 3,name: "Charlie",firstname: "Charles"}];// Using the map method to extract all the id into an arrayletids=users.map(user=>user.id);// Checking if an external id exists in the extracted Id array with the includes functionlettargetId=2;letidExists=ids.includes(targetId);console.log(idExists);// Output: true