Extract NodeJS Response into HTML - javascript

I want to take this request on NodeJS
Stripe.apiKey = "sk_test_AJKebDsM6QFiK8VpJ06Vq1M1";
stripe.balance.retrieve(function(err, balance) {
// asynchronously called
});
which an example response is
{
"pending": [
{
"amount": 0,
"currency": "usd"
}
],
"available": [
{
"amount": 0,
"currency": "usd"
}
],
"livemode": false,
"object": "balance"
}
and extract the balance information to place it into an HTML label.
How would I go about doing this? Can I pass the response to Angular and then link that to HTML?

You have to have that being served in a route (something like /balance) and then have your angular app consuming it:
If you're using express as your web framework, here's an example (from the top of my head - not tested):
app.get('/balance', function (request, response) {
stripe.balance.retrieve(function(err, balance) {
response.send(balance);
});
});
and here your angular app:
angular.module('myApp', [])
.controller('BalanceController', function ($scope, $http) {
$http.get('/balance').then(function (response) {
$scope.balance = response.data;
});
});

Related

how to output to servicebus from httptriggered function

How do we bind output to service bus?
I've set up an out binding inside of my azure function:
{
"queueName": "testqueue",
"connection": "MyServiceBusConnection",
"name": "myQueueItem",
"type": "serviceBus",
"direction": "out"
}
I've started out with the standard javascript/typescript template for the function:
export function run(context: any, req: any): void {
context.log("TypeScript HTTP trigger function processed a request.");
context.log(req.query);
context.bindings.outputSbQueue = req.query; //should bind here
if (req.query.name || (req.body && req.body.name)) {
context.res = {
// status: 200, /* Defaults to 200 */
body: {
message: `Hello ${(req.query.name || req.body.name)}`
}
};
} else {
context.res = {
status: 400,
body: "Please pass a name on the query string or in the request body"
};
}
context.done();
};
I'm getting a response from the function, but nothing is going into service bus.
I've got my app settings for MyServiceBusConnection set up, as the portal shows:
What am I doing wrong? How do we bind the output to service bus?
Here's the full function.json file:
{
"disabled": false,
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"queueName": "testqueue",
"connection": "MyServiceBusConnection",
"name": "myQueueItem",
"type": "serviceBus",
"direction": "out"
}
],
"scriptFile": "../dist/HttpTriggerTS/index.js"
}
Why the name of your binding (myQueueItem) is different from the way you call it in code (context.bindings.outputSbQueue)?
The following example shows a Service Bus output binding in a function.json file and a JavaScript function that uses the binding. The function uses a timer trigger to send a queue message every 15 seconds.
Here's the binding data in the function.json file:
{
"bindings": [
{
"schedule": "0/15 * * * * *",
"name": "myTimer",
"runsOnStartup": true,
"type": "timerTrigger",
"direction": "in"
},
{
"name": "outputSbQueue",
"type": "serviceBus",
"queueName": "testqueue",
"connection": "MyServiceBusConnection",
"direction": "out"
}
],
"disabled": false
}
and here is the sample JS code
module.exports = function (context, myTimer) {
var message = 'Service Bus queue message created at ' + timeStamp;
context.log(message);
context.bindings.outputSbQueue = message;
As pointed out by Farrukh, please ensure you are using the correct name in the binding and in the code.(outputSbQueue )
Hope it helps.

How to pass the array object in $http.get method in angularjs

I want to pass Array object in $http.get(). Previously all information is stored in data.json file, But I don't want to use file. Want to define the Array of object as $scope.data in controller.
Please find DEMO
http://plnkr.co/edit/X5ZC3UGey4Zjos7W01U1?p=preview
Working DEMO http://plnkr.co/edit/o6EeKnC8oFEq3GCRsQgp?p=preview
here we are using data.json. I want to define data inside data.json in Controller, Plz tell me how to dd
.controller('QuestionCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.questions = [];
$scope.data = {
"questions": [
{
"question": "Qw1",
"answers": [
{
"answers": "An1",
"value": 25
},
{
"answers": "An2",
"value": 50
},
{
"answers": "An3",
"value": 75
},
{
"answers": "An4",
"value": 100
}
]
},
{
"question": "Qw2",
"answers": [
{
"answers": "An1",
"value": 25
},
{
"answers": "An2",
"value": 50
},
{
"answers": "An3",
"value": 75
},
{
"answers": "An4",
"value": 100
}
]
}
]
}
$http
.get(JSON.stringify($scope.data))
.then(function(response){
$scope.questions = response.data.questions;
});
}])
I'm using this, and it works great for me
$http.get('url', {params: yourArray}).then(function (result) {
//do sth with data
});
HTTP GET request can't contain data to be sent to the server. You can add a query string to the request. $http option for it is params.
$http({
url: 'example.com',
method: "GET",
params: {...}
});
You dont need to use $http.get() if the data is directly available in controller.
You can use $scope.data instead of $scope.questions. ie in your html file use
{{data.questions[0].question}}
<h2>answer 1</h2>
<p>{{data.questions[0].answers[0].value}}</p>
<p>{{data.questions[0].answers[0].answers}}</p>
I have tried this way and it is working fine:
For example in your controller you have any array like arr variable
var data = Array;
data['Id'] = "1";
data['Name'] ="Test User";
data['UserName'] = "test_user";
data['Email'] = "test#gmail.com";
data['Address'] = "Test Address";
data['Action'] = 'Create';
To pass this array you can use like this
$scope.users = data;
The controller send an array in view page as the variable users
and you can use something like this
ng-repeat="user in users"
Note: this will work for two dimensional array and one dimensional array.
AngularJS has $http service, to get data asynchronously. So it can return any type of data(variable, array, string).
It is better to store data in some common place. Let me show factory pattern which is used for sharing data between controllers.
At first, you should create yourFactory.js file and write the following code:
(function () {
var yourFactory = function () {
var persons = [
{
id: 1,
name: 'Jon',
},
{
id: 2,
name: 'Ben',
},
{
id: 3,
name: 'Joseph',
},
{
id: 4,
name: 'Adam',
}
];
var factory = {};
factory.getPersons = function () {
return persons;
};
return factory;
};
angular.module('personsApp').factory('personsFactory', yourFactory);
}());
and some your controller can use data(just create new .js file for the following controller):
(function()
{
debugger;
var yourController=function($scope, yourFactory) {
/*alert('a1');*/
debugger;
function init() {
debugger;
$scope.yourData=yourFactory.getPersons();
}
init();
};
yourController.$inject=['$scope', 'yourFactory'];
angular.module('yourAppl').controller('yourController', yourController);
}());

Angular Two way binding for standard JSON?

Actually i want to make below JSON Two way binding in angular.
I am using the directive and Factory to bind the control(like TextField, AutomComplete, NumericTextBox..etc) dynamically.
Actually i tried many ways to bind the ng-model to control . it's not working. So please advice how to achieve this.
Source JSON
scope.sodata = JSON.parse (
{
"Data": {
"entityinfo": {
"entity": "Customer29Jan16",
"tenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
"timeStamp": "2016-04-11T13:46:32.708Z"
},
"collections": {
"Customer29Jan16": {
"meta": {
"parentreference": "***",
"pkname": "***",
"fkname": "***"
},
"rowset": [
{
"CuId": "",
"Name": "",
"Quantity": "",
"Rate": "",
"Amount": ""
}
],
"rowfilter": []
}
}
}
} );
In directive i am generating the Binding path using angular foreach
bindingPath = "scope.soData.Data.collections.Customer29Jan16.rowset."+index2;
Directive Two way binding i am giving like
layoutTableCellControlRenderObj.scope={attributeId:'=',controlId:'=',layoutData:'=',pageObject:'=',mapperData:'=', cellControlId:'=', soData:"="};
Factory;
bosAppModule.factory("layoutRenderingControlsFactory",function($compile){
var layoutControlsFactory={};
layoutControlsFactory.ThirdPartyReadOnly=function(controlId, bindingURL){
console.log("## Create ThirdPartyReadOnly");
var template='<input kendo-auto-complete ng-model="bindingURL">';
return template;
};
return layoutControlsFactory;
});
Please help any one to achieve this.
Thanks in advance.

Why AngularJS is changing values of the objects?

My AngularJS service code -
this.getEducation = function (id) {
var url = 'SOME_URL/?party_id=' + id;
var deferred = $q.defer();
$http.get(url).
success(function (data, status, headers, config) {
console.log(data);
deferred.resolve(data);
}).
error(function (data, status, headers, config) {
console.log("could not get education info");
deferred.reject(data);
});
return deferred.promise;
}
Now, my service is returning a data like this -
[
{
"id": 22,
"party_id": 9,
...
"university": "UoP",
"created_at": "2015-07-13 17:09:52",
"degree": "BE"
},
{
"id": 23,
"party_id": 9,
...
"university": "UoP",
"created_at": "2015-07-13 17:11:06",
"degree": "ME"
}
]
Now, here's the problem - when the data being resolved in promise, contains following array -
[
{
"id": 22,
"party_id": 9,
...
"university": "UoP",
"created_at": "2015-07-13 17:09:52",
"degree": "BE"
},
{
"id": 23,
"party_id": 9,
...
"university": null,
"created_at": "2015-07-13 17:11:06",
"degree": null
}
]
So, my question is, WHY AngularJS setting some values of my array elements to null ???
P.S. I'm using the data retrieved by this in controller, assigning it to scope variable, and doing ng-repeat on forms.
Edit :
My controller code is as follows
$scope.educationInformations = [];
$scope.setEducation = function () {
EducationProvider.getEducation(id).then(
function (educations) {
angular.forEach(educations, function(education){
console.log(education);
$scope.educationInformations.push({education:education});
console.log($scope.educationInformations);
})
});
};
THIS works, (console log is accurate)
Now, This is my template code.
When this is used,
<div ng-repeat="educationInfo in educationInformations">
<input-text ng-model="educationInfo.education.university"></input-text>
</div>
Now input-text is a directive created by me..
Directive code -
.directive('inputText', function () {
return {
templateUrl: 'views/inputText.html',
restrict: 'E',
scope: {
ngModel: '='
}
};
});
Directive template
<input class="form-control"
ng-model="ngModel" ng-init="ngModel = null">
Edit the directive template code as follows
<input class="form-control"
ng-model="ngModel" ng-init="ngModel">
Your code was setting ng-model to null ( ng-init = "ngModel = null ), as you have two way binding with controller, this initialization to null would affect the controller scope objects.

http json file retrieving with IONIC and AngularJS

I am working on an product reviewing app based on ionic framework.
The app will consume REST service in JSON format.
Because Im not able to retireve data directly from the locally deployed website, i created a json demo file:
[{
"index": 0,
"marque": "labore",
"estAutorise": 0,
"name": "Jennifer Simmons"
},
{
"index": 1,
"marque": "duis",
"estAutorise": 0,
"name": "Beatriz Hale"
},
{
"index": 2,
"marque": "pariatur",
"estAutorise": 1,
"name": "Richmond Garner"
}
]
here is my services.js:
angular.module('starter.services', [])
.factory('Recettes', function($http) {
var recettes = [];
var produits = [];
$http.get('test.json').success(function(data) {
alert("success"); //to check the seccess responce
produits= data;
});
return {
...
produits: function() {
return produits;
},
....
Before this, I implemented products and recettes directly and it works just fine. And now, I recieve the success alert but the data seems to be invalid.
My controller:
angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope, Recettes) {
$scope.produits = Recettes.produits();
})
...
So my question is how can i fix the data binding problem ?
And If it works with .json file should it work for rest services as well (Content-Type: application/json )?
Thank you.

Categories

Resources