Sending multiple data and receiving it though post in angular js - json

This is my code to post data
$scope.res=function(fina){
$scope.total= $scope.output;
$scope.finals=angular.copy($scope.fina);
$http.post('/submit', {order: $scope.finals,total: $scope.total}).success(function(data){
alert(data);
$scope.finals=data;
});
};
//load_groups();
$http.get('http://localhost:3070/submit').success(function(data){
alert("here too");
$scope.finals=data;
});
Can I post order and total in the same post? If yes how can I access it in get??
This is my app.get code. How do I send total in this?
app.get('/submit', function (req, res){
//TODO check that the data is valid before just using it
return res.json( req.session.order);
});

Related

Sending data with axios in html to use in intern JS file

I'm sending data from html using a button, I have the data and I want to pass it to an internal js because I don't want to expose credentials, but when I want to use it, it gives me an undefined instead of the data, that's why I need your help :c
Thanks in advance, below I show the code
My html file:
submitBtn.addEventListener("click", function (e) {
var folder_id = location.search.slice(1).split("&")[0].split("=")[1]
var document_id = location.search.slice(1).split("&")[1].split("=")[1]
axios.post('/send-signature',{
data: {
"document_id": document_id,
"folder_id": folder_id
}
})
}
My JS file
router.post("/send-signature", (req, res) => {
const data = req.body
console.log(data)
})
The error is that my req.body is undefined, that is, I think the data is not arriving.

How to pass information to the view after function completion when using NodeJS Request?

I'm writing code that would fetch some JSON data and pass it on to a new view with the given data.
The view is called /newbook2 and I want to pass information such as book name, author name and so on to the view from a JSON file called through an API.
var s_book_name;
var s_author_name;
var s_isbn13_id;
var s_publisher_name;
var s_category_id;
var error="";
router.post('/searchbook', function(req, res){
var isbn=req.body.isbn;
var url="http://isbndb.com/api/v2/json/63JEP95R/book/"+isbn;
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
if(body.data[0].title!=null)
s_book_name=body.data[0].title;
if(body.data[0].author_data[0]!=null)
s_author_name=body.data[0].author_data[0].name;
if(body.data[0].isbn13!=null)
s_isbn13_id=body.data[0].isbn13;
if(body.data[0].publisher_name!=null)
s_publisher_name=body.data[0].publisher_name;
if(body.data[0].subject_ids[0]!=null)
s_category_id=body.data[0].subject_ids[0];
}
else error="Book not found. Please enter the information manually."
});
res.redirect('/newbook2');
});
However, the information insn't yet loaded in my view. It seems like a common problem with asynchronous calls. However, I'm new to nodejs and would appreciate any help on how to fix it.
router.get('/newbook2', function(req, res){
res.render('newbook2', {title: 'Add New Book',s_book: s_book_name, s_author: s_author_name, s_isbn13: s_isbn13_id ,s_publisher: s_publisher_name , s_category: s_category_id});
});
You need to use promises for this. Here is some sample code for you to refer to make the POST call.
router.post('/newBook', function(req, res, next) {
console.log('Please add your here to get the data from server');
Obj.then(function() {
res.send('You got the success response from the server');
});
});
res.send('Your Book has been successfully added.'); will only execute after you get the success response from the server.
Please go through the concept of promises for more detail.

Nodejs page freezes after multple requests to Custom API

The problem I have is that my application works when I submit only one 1 when I press the Submit button multiple times it freezes and after some time (about 1000.000 ms) it returns the last request in the console and jade page. The submit button returns a post from the form and sends it to the same page . What the button also does is Refreshing the page. It is important that the page returns the (JSON) post to the page and there is an other json request that sends it to the API(and returns it to the same page )
app.js
var options_search = {
hostname: 'host',
path: 'path',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': JSON request .length,
}
};
app.post('/result',function(req,response){
var keyword=req.body.user;
GLOBAL.objToJson ={ keyword };
response.write(JSON.stringify(GLOBAL.objToJson));
console.log("test " +JSON.stringify(GLOBAL.objToJson) );
});
app.get('/Search', function(req, res) {
var req = http.request(options_search, (resb) => {
var buffer_search = "";
resb.setEncoding('utf8');
resb.on('data', (chunks) => {
buffer_search += chunks;
});
resb.on('end', () => {
res.render('Search',{
title: 'Search',
value_search: buffer_search,
search_test: [JSON.stringify(GLOBAL.objToJson) ]
});
});
});
//res.redirect("/search");
req.write(search);
});
search.jade
doctype html
html
head
script(src='//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js')
script.
$(document).ready(function(){
var user,pass;
$("#submit").click(function(){
user=$("#user").val();
pass=$("#password").val();
$.post("http://localhost:3000/result",{user: user}, function(data){
if(data==='done')
{
alert("login success");
}
});
});
});
input#user(type='TEXT', size='40')
br
input#submit(type='button', value='Submit',onclick="location.href='search'")
In the result route you are using the underlying HTTP .write() method to respond to the client. However this does not end the connection, which will stay open expecting more things to be written to the client.
If you are sending a string you should use .send() as that will write the string to the HTTP stream and end the response.
You may also want to consider not stringifying the object to a JSON string and just using .json() instead. So the line of code
response.write(JSON.stringify(GLOBAL.objToJson));
becomes
response.json(GLOBAL.objToJson);

How to display console.log results from API requests

//Express
var express = require('express');
var server = express();
//Steam
var SteamWebAPI = require('steamwebapi').SteamWebAPI;
SteamWebAPI.setAPIKey('XXXXXXXXXXXXXXXXXXXXXXXXXXXX');
//Steam - Recently Played Games
server.get('/games', function (req, res) {
SteamWebAPI.getRecentlyPlayedGames('76561198190043289', 5, function(response) {
res.json(response.response.games);
});
});
//Localhost
server.listen(3000, function () {
console.log('Server: ');
});
This is my output:
[{"appid":730,"name":"Counter-Strike: Global Offensive","playtime_2weeks":620,"playtime_forever":4016,"img_icon_url":"69f7ebe2735c366c65c0b33dae00e12dc40edbe4","img_logo_url":"d0595ff02f5c79fd19b06f4d6165c3fda2372820"}]
Ok, if you go to website: https://steamid.io/ ,you enter your name, press button 'lookup', and you get your IDs. How do I make that? Is it possible to make that user enter his name/id and he get informations. This way I'm only one who can enter user, and he gets text, images according to his request.
Like, I learned a lot about Steam Web Api these days but I still haven't figured out how to display data. Some cool guy helped me, but we used Angular, I'm still not familiar with it, I'm planning to learn Ember.js these days, but are they any alternatives?
If you wanna do as your example, it seems you miss to do the front part, and some back end too.
First you have to create a form. In your case, just an input to ask the user ID and a submit button could be enough. Then you have created your form in a html file, render it with the render function gave by Express framework (http://expressjs.com/en/4x/api.html#app.render)
And a light reminder on the html form (http://www.w3schools.com/html/html_forms.asp)
But you have to render it, to a specific URL, for example
// Render your basic form
// The form is in the form.html file
// By default, views have to be placed into the view or views folder, I don't know exactly no more
server.get('/ask_user_id', function (req, res) {
app.render('form', function(err, html){
if(err) return res.send(err);
else return res.send(html)
});
});
So if you go on localhost:3000/ask_user_id your form will be rendered
Second when a user write its ID into your input and submit the form, you have to retrieve theses info, and then call your steam API. So, if the action of your form is a POST, and the Url to submit is /user_infos, and the input for user ID's name is userId, here will be the code.
// Render your basic form
// The form is in the form.html file
// By default, views have to be placed into the view or views folder, I don't know exactly no more
server.get('/ask_user_id', function (req, res) {
app.render('form', function(err, html){
if(err) return res.send(err);
else return res.send(html)
});
});
// You have to have a route that handle your form submission
server.post('/user_infos', function (req, res) {
// All your form submitted is in your req.body
// So retrieve the userID
var _userID = req.body.userId;
// Then sent the variable to the SteamAPI
SteamWebAPI.getRecentlyPlayedGames(_userID, 5, function(response) {
return res.json(response.response.games);
});
});
Hope it helps
In your controller/Factory, make sure you have a callback function to get the response from the server.
$scope.postData = function(){
$http.post('/', {data: someData})
.then(function(response){
console.log(JSON.stringify(response.data));
});

Node Exports Function Returning Undefined

I have a an exports function I'm calling that should return a json array of draft results. In the route below in app.js, when I console.log draft_results, I get undefined
app.get('/draft-results', function(req, res) {
var draft_results = fantasy.getDraftResults(req, res);
console.log(util.inspect(draft_results, false, null));
//looks in views folder by default
res.render('draft-results', {
draft_results: draft_results
});
});
In my other file, this is the function that should be returning the json array. If i console.log draft, the data is there.
exports.getDraftResults = function(req, res, cb) {
oauth.get(
"http://fantasysports.yahooapis.com/fantasy/v2/league/" + conf.LEAGUE_ID + "/draftresults?format=json",
req.user.accessToken,
req.user.tokenSecret,
function(e, data, resp) {
if (e) console.error(e);
data = JSON.parse(data);
var draft = data.fantasy_content.league[1].draft_results;
res.json(draft);
}
);
};
I feel like I am returning the data incorrectly, and I can't seem to find any other good examples out there. Could someone please assist?
getDraftResults() is asynchronous. That means the results it generates occur sometime later. Thus, it cannot return its results directly from the function like you are trying to use.
It is unclear what you want to be doing here. Inside of getDraftResults() you are creating a JSON response back to the web request that started all this. That, in itself would be fine and will work as you have it (except the error handling is missing).
But, in your app.get() handler, you have completely different code that seems to thing that getDraftResults() is going to return a value (it has no return value at all) and then you will later use that return value.
So, if you just want getDraftResults to make a JSON response to the original web request, it's already doing that and you can remove the rest of what you have in the app.get() handler. If that's not really what you want to do and you want to use the response from getDraftResults() inside of the app.get() handler, then you will have to change the design of both functions and likely pass a callback to getDraftResults() so the callback can supply the asynchronous response and you can then continue the rest of the app.get() functionality in that callback.
If you're trying to do the latter, then here's a scaffolding (I don't know exactly what you're trying to accomplish so I can't be too detailed here):
app.get('/draft-results', function(req, res) {
fantasy.getDraftResults(req, function(err, draft_results) {
if (err) {
// send some sort of error response here
console.error(err);
return;
}
console.log(util.inspect(draft_results, false, null));
//looks in views folder by default
res.render('draft-results', {
draft_results: draft_results
});
});
});
exports.getDraftResults = function(req, cb) {
oauth.get(
"http://fantasysports.yahooapis.com/fantasy/v2/league/" + conf.LEAGUE_ID + "/draftresults?format=json",
req.user.accessToken,
req.user.tokenSecret,
function(e, data, resp) {
if (e) {
console.error(e);
cb(e);
return;
}
data = JSON.parse(data);
var draft = data.fantasy_content.league[1].draft_results;
// send results back to caller
cb(null, draft);
}
);
};