How to write API with multiple parameter in express.js [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm a bit new to Node.js and writing some GET REST APIs in Express.js. For one of my API, I need to pass 2 parameters to get the data from the database. I have achieved it using a single parameter but not more than 2. anyone help me, please?
Here my function
getByNPPTahunBulan(req,res){
console.log(req.param.tahun_bulan)
Iuran_Dapen.findAll({
where: {
tahun_bulan: req.params.tahun_bulan,
npp: req.param.npp
}
}).then((data) => {
return res.send(data);
}).catch((err) => {
res.status(500).send({
message: err.message || 'Some error occurred while retrieving data.',
});
})
},
and the url router.get('/iuranterakhir/:tahun_bulan/:npp', iuranDapen.getByNPPTahunBulan);

req.params contains a parameter object with its respective value with the following url /:id/:name
ej.
{
'id': 1,
'name': 'foo'
}
and you can do something like this:
const params = req.params;
and access as follows
params.id or params.name
the keys of the objects depends on the name of the parameters that you have given in the definition of your endpoint

Related

what's the order of data when I try to get data from supabase? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 hours ago.
Improve this question
I'm using supabase database to store my data, here is one table:
myTable, then I try to use fetch method in JavaScript to get data back, the code are as follows:
const res = await fetch('-', {headers: {apikey: '-',authorization: 'Bearer -',}}); fieldVal = await res.json();
(I have omitted the link)
Then I got the data back, but the order of the data confused me,
data
So, why the data of id equals 5 is the last data? Shouldn't be after the data of id equals 4?
This is my first time to use supabase, and I have scanned the docs and API, but it's a lot of other stuffs, I can't precisely locate the information I need.
You can order your query using supabase-js: https://supabase.com/docs/reference/javascript/order
const { data, error } = await supabase
.from('countries')
.select('id', 'name')
.order('id', { ascending: false })

SuperHero API - Access Token [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm trying to retrieve a json response from SuperHero API but can't find where to create an access token anywhere.
When trying to test a request on the website it says I need to log in via Facebook but there's no option to do that.
Is the Api still active?
Anyone has an access token I can use?
right on the front page you have the Facebook button to login
if you don't see it, check if you have any plugin in your browser that might be blocking it, and also, might be a good idea to see if you see the button in a private window
after accepting the FB web application, you will get the token
and will be a valid token even if you logout
here's the output when getting info for MichaelAngelo character:
https://www.superheroapi.com/api.php/101617...9/450
{
"response":"success",
"id":"450",
"name":"Michelangelo",
"powerstats":{
"intelligence":"63",
"strength":"14",
"speed":"50",
"durability":"65",
"power":"59",
"combat":"75"
},
"biography":{
"full-name":"",
"alter-egos":"No alter egos found.",
"aliases":[
"Mike",
"Mikey",
"Mikester",
"Michaelangelo"
],
"place-of-birth":"-",
"first-appearance":"-",
"publisher":"IDW Publishing",
"alignment":"good"
},
"appearance":{
"gender":"Male",
"race":"Mutant",
"height":[
"-",
"0 cm"
],
"weight":[
"- lb",
"0 kg"
],
"eye-color":"Blue",
"hair-color":"-"
},
"work":{
"occupation":"-",
"base":"-"
},
"connections":{
"group-affiliation":"-",
"relatives":"Leonardo (brother), Donatello (brother), Raphael (brother)"
},
"image":{
"url":"https://www.superherodb.com/pictures2/portraits/10/100/10329.jpg"
}
}

How can I get data from the url and parse it for getting hobbies or names? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I tried so many things and I researched lots of sites. But I can't find that how to parse this data without using modules. I am using node.js for this. My problem is I can't get the data from url to parse it. I hope my question is clear now. Thanks for answers.
const https = require("https");
https.get("https://coderbyte.com/api/challenges/json/rest-get-simple", (resp) => {
}
);
The documentation of the https.get function provides you with an example. Does that help you?
const https = require('https');
https.get("https://coderbyte.com/api/challenges/json/rest-get-simple", (res) => {
res.on('data', (d) => {
obj = JSON.parse(d.toString());
console.log("Hobbies",obj.hobbies);
});
}).on('error', (e) => {
console.error(e);
});
So within the get callback you need a listener on if some data was received.

JSON.stringify is not a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
REMARK: I've been searching for an answer for 3 hours, do not mark this as a duplicate as no answer is available.
I'm writing a simple API in Node.js using Express. When dealing with some of my front-end JS I use JSON.stringify() to convert my 'data' object into JSON. This is the function that does this:
function JSON(){
let radicando = document.getElementById("radicando-errore").value;
let iterazioni = document.getElementById("ripetizioni-babilonese").value;
const data = {
radicando: radicando,
iterazioni: iterazioni
};
var json = JSON.stringify(data);
const options = {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: json
};
fetch("/", options);}
Basically I obtain the input from these two input boxes:
<input type="number" id="radicando-babilonese" placeholder="Radicando"> <br><br>
<input type="number" id="ripetizioni-babilonese" placeholder="Ripetizioni massime"> <br><br>
And then proceed to insert it into the 'data' object and stringify it. However, I get the following error in the Chrome console:
Uncaught TypeError: JSON.stringify is not a function
I've tried changing many things around. I've even tried using several examples from Mozilla Developer Network and other StackOverflow answers but non onf them work.
Could it be that I use 'let' and not 'const' for the data? Could the fetch() URL not be correct and so the error backtracks? The function I placed above is the only one in my JS file and the button that calls it is:
<button type="button" class="calcola" onclick="JSON()">Calcola!</button> <br><br>
Any ideas would be greatly appreciated. I'll also append the app.js Express/Node.js file:
const express = require("express");
const path = require("path");
const body = require("body-parser")
const app = express();
const PORT = 8080;
app.use(express.static(__dirname + "/public"));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "/public/html/index.html"));
});
app.listen(PORT, () => {
console.log("App listening on:${PORT}");
})
JSON (try it out yourself in console) is an already existent native object in JS. - So basically don't name your function JSON if it actually does something completely different :D - perhaps postData() would better fit
On the first line your function name is JSON it might be causing issue, Try renaming it to something else.

How to call Zillow API with JavaScript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm new to APIs and am trying to figure out how to make a Zillow call with JavaScript, specifically the "getsearchresults".
Thanks
You'll need to use Javascript on the server-side using Node. It won't be possible to call the API on the front-end with Javascript. See this answer.
For using Node to call the Zillow API, check out the node-zillow package. Here's an example of how to use it with the GetSearchResults API call:
const Zillow = require("node-zillow")
const zillow = new Zillow('your key here')
const parameters = {
address: "2114 Bigelow Ave",
citystatezip: "Seattle, WA",
rentzestimate: false
}
zillow.get('GetSearchResults', parameters)
.then(results => {
console.log(results)
return results
})
Make sure you signup for ZWSID here: https://www.zillow.com/webservice/Registration.htm. You'll use this unique id when making the request to the API. It'll look something like this: X2-Ijdjkxlujnkd_jske2. Keep it secret, keep it safe!

Categories