how to repeat get request every values change - node.js

I have codes like
app.post('/zamansecimi',(req,res)=>{
{
zamansecimim(req.body.baslangic,req.body.bitis)
res.render('./dist/rapor.ejs')
}});
function zamansecimim (bas,bit) {
app.get("/zamansecimi2", function (req, res) {
data2="select * from raporlama where oncekizaman between $1 and $2"
var query = connection2.query(data2,[bas,bit] ,
function(err, result) {
if(err) throw err;
res.send(httpResponse(result.rows));
});
})
}
In these codes I want to change get result every post request but I have a problem with get request these values never change when I send to 2 or 3 ...request. First request is working.
Where is my mistake?
thank you for your help

Each request sent to the server can only have one response, and you can't trigger a GET request inside a POST request on the server and expect the response to that request to go back to the browser. Without the whole app, I can't tell you the best way to do what you want, but I'd suggest you have one response on the server like this:
app.post('/zamansecimi',(req, res)=>{
const bas = req.body.baslangic;
const bit = req.body.bitis;
const query="select * from raporlama where oncekizaman between $1 and $2";
connection2.query(query,[bas,bit], function(err, result) {
if(err) throw err;
res.send(httpResponse(result.rows));
});
});
That will at least ensure you get the correct data back to the browser. It does not seem to me like rapor.ejs needs re-rendering since nothing has changed on the server with the POST request. But honestly I can't tell what's going on from the code provided.

Related

Unable to get data from React App to Node server

So Am unable to make a search function i want to get a variable from search field and show the results that matched but am constantly getting this error
variable undefined when i try to console.log it in the node server
Edit-- i have already changed the axios.post to axios.get
app.get(`/search/`, (req, res) => {
let {name} =req.body
var Desc = name
console.log(name)
var Op= Desc+'%'
const q = "SELECT * FROM taric where Description LIKE ? ";
con.query(q,[Op], (err, search) => {
if (err) {
console.log(err);
return res.json(err);
}
console.log(search);
return res.json(search);
});
});
As you can see you are making POST request from frontend where as there is no POST request route to handle your request. As you have make route of GET for fetching the data from backend you need to make GET request from frontend as well. So you need to do as below:
axios.get(`your_endpoint_route_goes_here`);
instead of this:
axios.post(`your_endpoint_route_goes_here`, requestBodyObj);
HTTP methods are not the same.
You are using app.get in the server while triggering a POST call from your client.
axios.post <-----> app.get
(There is no route for POST call which client is expecting)

Axios - send get request with url + variable

This must be a stupid question, but I'm just starting and would appreciate any help!
So I have this code to get query parameter:
app.get('/', (req, res) => {
var code = req.query.code;
console.log(code);
And when I go to http://localhost:3000/?code=123, I get the code value in console, so it works fine.
Now, I need to send a GET request and add the value of the var code, this is where I'm stuck.
Let's say, I should send a GET request to 'http://testtesttest123.com/' + var code + 'hi'.
How can I do this?
I've tried this way and some other ways, but nothing worked:
axios.get('http://testtesttest123.com/?&code=', {params: {code}}, '&hi')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
});
Thank you in advance!
The axios.get call should look like this.
axios.get('http://testtesttest123.com/?code=' + code + '&hi')
With code = 123, this will call the URL http://testtesttest123.com/?code=123&hi.
Use the params config to send through query parameters. To support your empty hi parameter, you can include it in the URL string
axios.get("http://testtesttest123.com/?hi", {
params: { code }
})
For a code value of 123, this will perform a GET request to
http://testtesttest123.com/?hi&code=123
It will also ensure that the code value is made safe for use in URLs

stopping postman after request is done

I am trying to create a Weather API using node. In my controller file, I have this code which is run for the /check route.
controller.js:
//Check Weather
exports.check = (req, res) => {
UserModel.check(req.body.city)
};
model.js:
//Check Weather
function getData(city) {
url = "something";
request(url, function (err, response, body) {
if(err){
console.log('error:', error);
} else {
console.log('body:', body);
}
});
}
exports.check = (city) => {
city = city.toLowerCase();
let values = getData(city);
console.log(city);
return(values);
};
route:
app.post('/check', [
UsersController.check
]);
When I run this, it functions properly and the correct thing is logged in the console. However, after I send a request in Postman and the console.log shows up, Postman seems to be hung up as seen in this pic. Is there anyway I can make it so that Postman stops sending the request after return or console.log?
Postman is waiting for a response from the server. Your code is not currently sending any response, so postman seems 'hung up' as it is waiting. Try changing the line saying UserModel.check(req.body.city) to say res.send(UserModel.check(req.body.city)) so it will send the data returned from your UserModel.check function back as the response. Alternatively, if you don't want to send back the returned value, you could just add res.send(PutWhateverYouWantSentHere) after the function call.

Sending Search parameters in get request for angular 4

I've searched the web. I found a couple of things but I wasn't able to understand anything out.
I'm trying to send a get request from my angular to Node.js server. I want to send some search parameters for the my server. and it searches then returns the documents.
I'm tring this function to get send the request. But I don't know how to ad parameters beside this request.
GetCases(){
return this.http.get('/api/saleCases')
.map((response:Response)=>response.json());
}
In the server side
router.get('/saleCases', function(req, res){
console.log('get request for all cases');
Case.find({}).exec(function(err, cases){
if(err){
console.log("error retriving videos");
}else{
res.json(cases);
}
});
});
Can you help me to add the parameters in angular and read them in node.js? Or is there any kind of way that you can suggest me to use?
In Client side, first init your search parameters, something like:
const searchParams = {
params: {
param1: val1,
param2: val2,
...
}
}
then send your request to Server side like:
return this.http.get('/api/saleCases', searchParams)
In Server side you can access the params, using:
router.get('/saleCases', function(req, res){
const param1 = req.query.param1;
const param2 = req.query.param2;
});

Response headers of previous request affecting current request

The following code is the user-facing part of a new node app we are building:
var loadInvoice = function(req, res, next) {
Invoice.findById(req.params.invoiceId, function (err, invoice) {
if (err) {
res.send(404, 'Page not found');
} else {
req.invoice = invoice;
next();
}
});
};
app.namespace('/invoices/:invoiceId', loadInvoice, function () {
app.get('', function(req, res){
var templateVals = {
//some template data
};
res.render('paymentselection', templateVals);
});
app.post('', function(req, res){
var data = {
// some data for the apiCall
};
someAPI.someRequest(data, function(err, data) {
console.log(res.status());
res.redirect(data.url);
});
});
});
The first method returns a confirmation page where the user presses a button to post to the same url, which triggers a redirect to an external website.
This all works exactly once. Every second request will crash the app with the message Cant set headers after they are sent. After carefull inspection of the code I could find no reason for this to happen so I added the console.log line which indeed confirms the location header has been set. But it is set to the value i got from someAPI on the previous request not the current one.
This makes absolutely no sense to me. I do not store this value anywhere nor do I do caching or persistence of this data in any way.
Does anybody know what could be causing this?
I use express, express-namespace, mogoose and swig
I found out the problem was being caused bij the 'Restler' libaray used within 'someAPI'. I have no idea how this is possible but swapping it out with something else fixed the problem.

Resources