Trouble populating Mongo Database from Nodejs - angularjs

The following json order has shipping, payment, orderProduct as elements. I am trying to access the individual elements using the code below:
{
"shipping": "{\"nickName\":\"gsfsdfdf\",\"firstName\":\"gsfsdfdf\",\"lastName\":\"gsfsdfdf\",\"addressLine1\":\"gsfsdfdf\",\"addressLine2\":\"gsfsdfdf\",\"city\":\"gsfsdfdf\",\"country\":\"gsfsdfdf\",\"province\":\"gsfsdfdf\",\"postalCode\":\"gsfsdfdf\",\"phone\":\"gsfsdfdf\",\"email\":\"gsfsdfdf\",\"shippingInstruction\":\"gsfsdfdfgsfsdfdfgsfsdfdfgsfsdfdf\"}",
"payment": "{\"cardType\":\"Visa\",\"cardNumber\":\"11111111111111111111\",\"expMonth\":\"3\",\"expYear\":\"2019\",\"csc\":\"111\"}",
"orderProduct": "[{\"productId\":\"594fce45734d1d24b915a41f\",\"quantity\":1}]",
"deliveryDate": "2017-07-07T04:00:00.000Z"
}
I am using the code below to access the various elements of the json data:
router.post('/order/add', function (req, res) {
console.log('order = ' + req.body.order);
var newOrder = req.body.order;
console.log('shipping = ' + newOrder.shipping);
console.log('payment = ' + newOrder.payment);
console.log('orderProduct = ' + newOrder.orderProduct);
console.log('deliveryDate = ' + newOrder.deliveryDate);
var order = new Order({
shipping: newOrder.shipping,
payment: newOrder.payment,
orderProduct: newOrder.orderProduct,
deliveryDate: newOrder.deliveryDate,
});
console.log("created server order = " + order);
return res.json(200, order);
})
But the shipping, payment, orderProduct, deliveryDAte are return undefined.

Related

How to map and send emails to specific users who don't meet a certain criteria in Javascript/GAS

I am new in Javascript and bit by bit I have used resources here on StackOverflow to build on a project that uses external API to get time entries for users from the 10k ft project management system. I have finally have different functions together as follows:
Calls for user details which includes user_id
Get the time entries and sums up for every user who's approval has a value (pending or approval) in a specific date range. Those without approval will be ignored in the summation and their total entries left at 0.
My challenge now is to have only those with 0 as total hours of time entries receive emails to update their time entries. This code doesn't seem to select only those with 0 and send emails specifically to them. I will appreciate any pointers and/or assistance. after sending the email, this should be recorded on Google sheet
var TKF_URL = 'https://api.10000ft.com/api/v1/';
var TKF_AUTH = 'auth'
var TKF_PGSZ = 2500
var from = '2020-01-20'
var to = '2020-01-26'
var options = {
method: 'get',
headers: {
Authorization: 'Bearer ' + TKF_AUTH
}
};
function getUsers() {
var userarray = [];
var lastpage = false;
var page = 1;
do {
// gets 10kft data
var users = read10k_users(page);
// writes data from current page to array
for (var i in users.data) {
var rec = {};
// pushing of mandatory data
rec.id = users.data[i].id;
rec.display_name = users.data[i].display_name;
rec.email = users.data[i].email;
userarray.push(rec);
}
// checks if this is the last page (indicated by paging next page link beeing null
if (users.paging.next != null) {
lastpage = false;
var page = page + 1;
} else {
lastpage = true;
}
}
while (lastpage == false);
return (userarray);
return (userarray);
}
function read10k_users(page) {
var endpoint = 'users?';
var url = TKF_URL + endpoint + 'per_page=' + TKF_PGSZ + '&auth=' + TKF_AUTH + '&page=' + page;
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response);
//Logger.log(json.data)
return (json);
}
function showTimeData() {
var users = getUsers()
var time_array = [];
for (var i = 0; i < users.length; i++) {
// Logger.log(users[i].id)
var url = 'https://api.10000ft.com/api/v1/users/' + users[i].id + '/time_entries?fields=approvals' + '&from=' + from + '&to=' + to + '&auth=' + TKF_AUTH + '&per_page=' + TKF_PGSZ;
var response = UrlFetchApp.fetch(url, options);
var info = JSON.parse(response.getContentText());
var content = info.data;
var total_hours = 0;
for (var j = 0; j < content.length; j++) {
if (content[j].approvals.data.length > 0) {
total_hours += content[j].hours;
}
}
Logger.log('User name: ' + users[i].display_name + ' ' + 'User id: ' + users[i].id + ' ' + 'total hours: ' + total_hours+ ' ' + 'Email: ' + users[i].email)
}
}
function sendMail(showTimeData){
var emailAddress = user.email;
var message = 'Dear ' + user.display_name + 'Please update your details in the system'
var subject = ' Reminder';
MailApp.sendEmail(emailAddress, subject, message);
}
I was able to get a solution for this as follows:
for (var j = 0; j < content.length; j++) {
if (content[j].approvals.data.length > 0) {
total_hours += content[j].hours;
}
}
Logger.log('User name: ' + users[i].display_name + ' ' + 'User id: ' + users[i].id + ' ' + 'total hours: ' + total_hours + ' ' + 'Email: ' + users[i].email)
if (total_hours == 0) {
sendMail(users[i])
}
}
}
function sendMail(user) {
var emailAddress = user.email;
var message = 'Dear ' + user.display_name + 'Please update your details in the system'
var subject = ' Reminder';
MailApp.sendEmail(emailAddress, subject, message);
}

How to update Salesforce through NetSuite?

How would you go about updating Salesforce with NetSuite data?
I know that you would need to use Suitescript 2.0 and some sort of token authentication.
I have a suitescript 2.0 user event script that does just this, It makes a connection to salesforce and updates netsuite customer fields to salesforce accounts.
The script will update three custom fields on the salesforce account: NSID, sync, syntime.
/**
*#NApiVersion 2.x
*#NScriptType UserEventScript
*/
define(['N/https', 'N/search', 'N/record'],
function (https, search, record)
{
_HTTPS = https;
_SEARCH = search;
_RECORD = record;
//handle after submition of customer on create and edit
function afterSubmit(context)
{
var body;
var NSID;
var sfEntityID;
var sync;
var syncTime;
var accountURL;
var response;
var recordData = {};
//If customer is getting deleted reflect that to SF
if(context.type === "delete")
NSID = 'deleted';
else
NSID = context.newRecord.id;
//get the SF account ID
sfEntityID = context.newRecord.getValue({fieldId:"custentity_sf_account_id"});
if (sfEntityID == undefined || sfEntityID == "") //If there is no SFID stop
{
log.debug("afterSubmit","No sfEntityID on customer with NSID: " + NSID);
return;
}
var date = new Date();
var month = date.getUTCMonth() + 1; // jan = 0
if (month < 10)
{
month = "0" + month;
}
syncTime = date.getUTCFullYear() + '-' + month + '-' + date.getUTCDate() + 'T' + date.getUTCHours() + ':' + date.getUTCMinutes() + ':' + date.getUTCSeconds() + '.000Z';
//login to SF (loginSalesforceNLAP controls sandbox/production login)
body = loginSalesforceNLAP();
//check if I got an access token back
if(body.access_token == undefined || body.access_token == "")
{
log.debug('afterSubmit','could not get SF access token');
return;
}
//set fields that will be updated on SF account
recordData.Netsuite_Internal_ID__c = NSID;
recordData.Sync__c = true;
recordData.Synced_Time__c = syncTime;
recordData = JSON.stringify(recordData);
//send update request
response = _HTTPS.post({
url: (getURL(body) + "/sobjects/Account/" + sfEntityID + "?_HttpMethod=PATCH"),
body: recordData,
headers: {"Authorization": "OAuth " + body.access_token,"Content-Type": "application/json"}
});
log.debug("response",response);
//log an error if it occurred
if(response.code != 204)
{
log.debug("afterSubmit","Could not update sf Account: " + sfEntityID + ' NSID: ' + NSID);
return;
}
//success message
log.debug("afterSubmit","successfully updated sf Account: " + sfEntityID + ' NSID: ' + NSID);
return;
}
return {afterSubmit: afterSubmit};
});
//get max SF version for SF update request
function getURL(body)
{
var max
var arr
var header = {"Authorization": "OAuth " + body.access_token };
var recordData = {};
var url = body.instance_url + "/services/data/";
response = _HTTPS.get({
url: url,
body: recordData,
headers: header
});
if(response.code == 200 || response.code == 204)
{
arr = JSON.parse(response.body)
for(var i = 0; i < arr.length; i++)
{
//find max version
if(!max || parseInt(arr[i]["version"]) > parseInt(max["version"]))
max = arr[i];
}
return body.instance_url + max.url;
}
else
return "";
}
//Connect to Salesforce instance and obtain the Access Token used for subsequent Salesforce calls this session
function loginSalesforceNLAP()
{
//production
var clientID = "3MVG9QDx8IX8nP5SpP0endmsendmeMepopopBvuB074i_7h7fakehoB0hnnhK7FyfTUgxH2234vR6QPoVXpDE";
var clientSecret = "1231232320412308455";
var securityToken = "N0bx9dI321F732aO12iC33gm6";
var username = "sflogin#login.com";
var password = "password";
var loginURL = "https://login.salesforce.com/services/oauth2/token";
var header = [];
header['Content-Type'] = 'application/json;charset=UTF-8';
var recordData = {};
var url = loginURL + "?grant_type=password&client_id=" + clientID + "&client_secret=" + clientSecret + "&username=" + username + "&password=" + password + securityToken;
try
{
response = _HTTPS.post({
url: url,
body: recordData,
headers: header
});
response = JSON.parse(JSON.stringify(response));
if (response.code == 200 || response.code == 204)
return JSON.parse(response.body); //return body
}
catch (er02)
{
log.error('ERROR:loginSalesforceNLAP', er02);
}
return "";
}
In order to get clientID, clientSecret, and the security token follow these steps: https://developer.salesforce.com/forums/?id=906F0000000AfcgIAC

Sort JSON file with NODEJS

I just started using NodeJS (as of yesterday) I have managed to retrieve JSON data from an API as seen in the code below:
var request = require('request');
var fs = require('fs');
const pug = require('pug');
request.get('APICALL', function(err, data) {
if (err) { }
else {
console.log(data);
fs.writeFile('industries.txt', JSON.stringify(data));
}
});
The JSON data that my request.get returns can be found here JSON FILE
What I want to do is sort through this data, find out which revenue and industry ID appears the most (i.e for this data it's "revenue:8" and "industry:69") once this is done I want to return only companies who match both revenue 8 and industry 9.
Is this possible and what would be the best way to approach such a task?
You can count the revenue - industry_id occurences, sort descending and then filter by that:
let arr = [{"id":1,"domain":"google.com","alexa_rank":1,"country":236,"employees":"7","revenue":"8","industry_id":"69"},{"id":3,"domain":"youtube.com","alexa_rank":2,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":2,"domain":"facebook.com","alexa_rank":3,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":4,"domain":"yahoo.com","alexa_rank":6,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":111,"domain":"reddit.com","alexa_rank":9,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":9,"domain":"twitter.com","alexa_rank":12,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":230,"domain":"360.cn","alexa_rank":20,"country":46,"employees":"5","revenue":"8","industry_id":"69"},{"id":30,"domain":"weibo.com","alexa_rank":22,"country":46,"employees":"5","revenue":"2","industry_id":"69"},{"id":19,"domain":"google.de","alexa_rank":23,"country":236,"employees":"7","revenue":"8","industry_id":"65"},{"id":23,"domain":"google.co.uk","alexa_rank":24,"country":236,"employees":"7","revenue":"8","industry_id":"45"},{"id":12,"domain":"linkedin.com","alexa_rank":25,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":25,"domain":"google.fr","alexa_rank":26,"country":76,"employees":"1","revenue":"2","industry_id":"65"},{"id":46,"domain":"google.ru","alexa_rank":27,"country":236,"employees":"0","revenue":"8","industry_id":"69"},{"id":33,"domain":"google.com.br","alexa_rank":28,"country":32,"employees":"3","revenue":"3","industry_id":"47"},{"id":22,"domain":"yandex.ru","alexa_rank":29,"country":182,"employees":"5","revenue":"7","industry_id":"69"},{"id":20,"domain":"google.com.hk","alexa_rank":31,"country":46,"employees":"0","revenue":"8","industry_id":"69"},{"id":101,"domain":"netflix.com","alexa_rank":32,"country":236,"employees":"5","revenue":"8","industry_id":"38"},{"id":35,"domain":"google.it","alexa_rank":34,"country":236,"employees":"6","revenue":"8","industry_id":"113"},{"id":3351716,"domain":"ntd.tv","alexa_rank":36,"country":236,"employees":"4","revenue":"0","industry_id":"13"},{"id":77,"domain":"imgur.com","alexa_rank":37,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":21,"domain":"ebay.com","alexa_rank":38,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":41,"domain":"google.es","alexa_rank":39,"country":236,"employees":"1","revenue":"2","industry_id":"65"},{"id":74,"domain":"pornhub.com","alexa_rank":40,"country":58,"employees":"1","revenue":"3","industry_id":"38"},{"id":13,"domain":"msn.com","alexa_rank":41,"country":236,"employees":"8","revenue":"8","industry_id":"69"},{"id":18,"domain":"wordpress.com","alexa_rank":43,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":507,"domain":"aliexpress.com","alexa_rank":44,"country":46,"employees":"7","revenue":"8","industry_id":"69"},{"id":42,"domain":"livejasmin.com","alexa_rank":47,"country":128,"employees":"1","revenue":"0","industry_id":"38"},{"id":28,"domain":"microsoft.com","alexa_rank":48,"country":236,"employees":"8","revenue":"8","industry_id":"25"},{"id":58,"domain":"google.ca","alexa_rank":49,"country":236,"employees":"7","revenue":"8","industry_id":"69"},{"id":44,"domain":"tumblr.com","alexa_rank":50,"country":236,"employees":"4","revenue":"5","industry_id":"69"},{"id":107,"domain":"stackoverflow.com","alexa_rank":52,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":2553,"domain":"twitch.tv","alexa_rank":53,"country":236,"employees":"4","revenue":"4","industry_id":"69"},{"id":40,"domain":"imdb.com","alexa_rank":58,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":488,"domain":"github.com","alexa_rank":61,"country":236,"employees":"4","revenue":"5","industry_id":"25"},{"id":239,"domain":"pinterest.com","alexa_rank":62,"country":236,"employees":"4","revenue":"4","industry_id":"69"},{"id":1853,"domain":"popads.net","alexa_rank":65,"country":53,"employees":"1","revenue":"1","industry_id":"84"},{"id":51,"domain":"xvideos.com","alexa_rank":66,"country":59,"employees":"0","revenue":"0","industry_id":"38"},{"id":7154677,"domain":"diply.com","alexa_rank":68,"country":40,"employees":"3","revenue":"0","industry_id":"100"},{"id":245,"domain":"csdn.net","alexa_rank":69,"country":46,"employees":"3","revenue":"2","industry_id":"69"},{"id":189,"domain":"wikia.com","alexa_rank":72,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":275,"domain":"google.com.tw","alexa_rank":74,"country":218,"employees":"0","revenue":"8","industry_id":"95"},{"id":8456,"domain":"whatsapp.com","alexa_rank":75,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":56,"domain":"xhamster.com","alexa_rank":78,"country":58,"employees":"1","revenue":"0","industry_id":"38"},{"id":44705842,"domain":"txxx.com","alexa_rank":81,"country":0,"employees":"1","revenue":"0","industry_id":"38"},{"id":2793171,"domain":"coccoc.com","alexa_rank":84,"country":243,"employees":"3","revenue":"0","industry_id":"65"},{"id":1000482,"domain":"bongacams.com","alexa_rank":85,"country":58,"employees":"0","revenue":"0","industry_id":"38"},{"id":92,"domain":"google.pl","alexa_rank":86,"country":236,"employees":"6","revenue":"8","industry_id":"77"},{"id":794,"domain":"pixnet.net","alexa_rank":86,"country":218,"employees":"3","revenue":"0","industry_id":"69"},{"id":249,"domain":"dropbox.com","alexa_rank":87,"country":236,"employees":"5","revenue":"7","industry_id":"69"},{"id":62718286,"domain":"porn555.com","alexa_rank":90,"country":0,"employees":"0","revenue":"0","industry_id":"38"},{"id":141,"domain":"google.co.th","alexa_rank":91,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":1215,"domain":"gmw.cn","alexa_rank":92,"country":46,"employees":"0","revenue":"0","industry_id":"97"},{"id":130,"domain":"google.com.pk","alexa_rank":93,"country":236,"employees":"3","revenue":"2","industry_id":"84"},{"id":317,"domain":"china.com","alexa_rank":98,"country":46,"employees":"5","revenue":"8","industry_id":"100"},{"id":148727,"domain":"amazon.in","alexa_rank":100,"country":236,"employees":"8","revenue":"8","industry_id":"69"},{"id":115,"domain":"google.com.ar","alexa_rank":101,"country":11,"employees":"0","revenue":"8","industry_id":"69"},{"id":375,"domain":"soundcloud.com","alexa_rank":103,"country":83,"employees":"4","revenue":"3","industry_id":"69"},{"id":39,"domain":"fc2.com","alexa_rank":104,"country":236,"employees":"0","revenue":"0","industry_id":"69"},{"id":62729524,"domain":"clicksgear.com","alexa_rank":104,"country":0,"employees":"0","revenue":"0","industry_id":"84"},{"id":2711,"domain":"bbc.com","alexa_rank":107,"country":235,"employees":"6","revenue":"8","industry_id":"13"},{"id":109,"domain":"google.nl","alexa_rank":108,"country":155,"employees":"7","revenue":"8","industry_id":"65"},{"id":146,"domain":"adf.ly","alexa_rank":110,"country":235,"employees":"1","revenue":"2","industry_id":"65"},{"id":7739,"domain":"uptodown.com","alexa_rank":111,"country":209,"employees":"1","revenue":"2","industry_id":"69"},{"id":26665,"domain":"ettoday.net","alexa_rank":111,"country":46,"employees":"0","revenue":"0","industry_id":"100"},{"id":240,"domain":"booking.com","alexa_rank":112,"country":155,"employees":"6","revenue":"8","industry_id":"69"},{"id":95,"domain":"dailymotion.com","alexa_rank":113,"country":76,"employees":"4","revenue":"5","industry_id":"69"},{"id":1022,"domain":"quora.com","alexa_rank":115,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":48,"domain":"ask.com","alexa_rank":116,"country":236,"employees":"4","revenue":"5","industry_id":"69"},{"id":2353727,"domain":"espn.com","alexa_rank":117,"country":236,"employees":"5","revenue":"7","industry_id":"13"},{"id":7170357,"domain":"blastingnews.com","alexa_rank":119,"country":216,"employees":"4","revenue":"2","industry_id":"100"},{"id":86,"domain":"nytimes.com","alexa_rank":120,"country":236,"employees":"5","revenue":"8","industry_id":"97"},{"id":1000001,"domain":"blogger.com","alexa_rank":124,"country":236,"employees":"7","revenue":"8","industry_id":"69"},{"id":106,"domain":"vimeo.com","alexa_rank":125,"country":236,"employees":"4","revenue":"5","industry_id":"69"},{"id":10725,"domain":"savefrom.net","alexa_rank":125,"country":182,"employees":"1","revenue":"0","industry_id":"69"},{"id":28040741,"domain":"daikynguyenvn.com","alexa_rank":126,"country":236,"employees":"0","revenue":"0","industry_id":"100"},{"id":547,"domain":"detik.com","alexa_rank":127,"country":103,"employees":"4","revenue":"5","industry_id":"100"},{"id":1282,"domain":"stackexchange.com","alexa_rank":129,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":200,"domain":"google.co.ve","alexa_rank":131,"country":236,"employees":"0","revenue":"8","industry_id":"69"},{"id":157,"domain":"google.co.za","alexa_rank":132,"country":205,"employees":"2","revenue":"2","industry_id":"82"},{"id":221,"domain":"salesforce.com","alexa_rank":132,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":2394,"domain":"vice.com","alexa_rank":135,"country":236,"employees":"5","revenue":"8","industry_id":"118"},{"id":4637,"domain":"tribunnews.com","alexa_rank":136,"country":103,"employees":"3","revenue":"2","industry_id":"87"},{"id":88,"domain":"ebay.co.uk","alexa_rank":138,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":177,"domain":"slideshare.net","alexa_rank":140,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":1816926,"domain":"theguardian.com","alexa_rank":141,"country":235,"employees":"5","revenue":"6","industry_id":"97"},{"id":1594,"domain":"spotify.com","alexa_rank":143,"country":215,"employees":"5","revenue":"8","industry_id":"95"},{"id":113,"domain":"xnxx.com","alexa_rank":144,"country":59,"employees":"1","revenue":"0","industry_id":"38"},{"id":236,"domain":"google.com.vn","alexa_rank":145,"country":243,"employees":"0","revenue":"8","industry_id":"9"},{"id":44263656,"domain":"adexchangeprediction.com","alexa_rank":146,"country":0,"employees":"0","revenue":"0","industry_id":"84"},{"id":884,"domain":"buzzfeed.com","alexa_rank":147,"country":236,"employees":"5","revenue":"5","industry_id":"69"},{"id":4263,"domain":"chaturbate.com","alexa_rank":148,"country":236,"employees":"2","revenue":"1","industry_id":"38"},{"id":161,"domain":"nicovideo.jp","alexa_rank":149,"country":111,"employees":"4","revenue":"5","industry_id":"69"},{"id":225,"domain":"google.gr","alexa_rank":149,"country":86,"employees":"1","revenue":"1","industry_id":"35"},{"id":158,"domain":"chase.com","alexa_rank":151,"country":236,"employees":"8","revenue":"8","industry_id":"44"},{"id":148,"domain":"mozilla.org","alexa_rank":153,"country":236,"employees":"5","revenue":"6","industry_id":"69"},{"id":723,"domain":"avito.ru","alexa_rank":153,"country":182,"employees":"4","revenue":"4","industry_id":"69"},{"id":87,"domain":"cnet.com","alexa_rank":155,"country":236,"employees":"4","revenue":"5","industry_id":"100"},{"id":224,"domain":"google.com.co","alexa_rank":156,"country":49,"employees":"7","revenue":"2","industry_id":"47"},{"id":340,"domain":"indeed.com","alexa_rank":157,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":558,"domain":"flipkart.com","alexa_rank":157,"country":102,"employees":"6","revenue":"8","industry_id":"69"}]
let mostOccurence = [...arr.reduce((a,b) => {
a.set(b.revenue + ":" + b.industry_id, a.has(b.revenue + ":" + b.industry_id) ? a.get(b.revenue + ":" + b.industry_id) + 1 : 1);
return a;
}, new Map)].sort((a,b) => b[1] - a[1])[0][0];
let split = mostOccurence.split(':'), revToLook = split[0], indToLook = split[1];
let filtered = arr.filter(e => e.revenue === revToLook && e.industry_id === indToLook);
console.log(filtered);

code passes through $scope.options first instead of $http.get

i have an ion-slide component with 3 slides. when running the app the first time, all three slides load. however, going to another controller and coming back to the controller where the ion-slide is (using $state.go), only displays one slide and has the following error:
TypeError: Cannot read property '0' of undefined
seeing this error, i traced it passes through this line first:
sharedProperties.setProperty($scope.cardNumbers[$scope.currentIdx]
.CardNumber);
instead of this code fetching the card number:
var url = 'http://10.10.9.169/UserService3/WebService1.asmx';
$http.get(url + '/getCardsbyUsername' + '?unameID=' + currentID ).success(function(response) {
// stuff
console.log('response is jsonobj = ' + response);
var strObj = JSON.stringify(response).replace(/"(\w+)"\s*:/g, '$1:');
var myObject = eval('(' + strObj + ')');
$scope.cardNumbers = myObject;
console.log('response is jsonarr = ' + $scope.cardNumbers);
})
.error(function(response) {
// error stuff
console.log('response error is = ' + response);
});
here's the full code:
$scope.currentIdx = 0;
var currentID = sharedProperties3.getUserID();
console.log('current ID = ' + currentID);
var url = 'http://10.10.9.169/UserService3/WebService1.asmx';
$http.get(url + '/getCardsbyUsername' + '?unameID=' + currentID ).success(function(response) {
// stuff
console.log('response is jsonobj = ' + response);
var strObj = JSON.stringify(response).replace(/"(\w+)"\s*:/g, '$1:');
var myObject = eval('(' + strObj + ')');
$scope.cardNumbers = myObject;
console.log('response is jsonarr = ' + $scope.cardNumbers);
})
.error(function(response) {
// error stuff
console.log('response error is = ' + response);
});
$scope.options1 = {
initialSlide: 0,
onInit: function(slider1)
{
$scope.slider1 = slider1;
sharedProperties.setProperty($scope.cardNumbers[$scope.currentIdx].CardNumber);
},
onSlideChangeEnd: function(slider1)
{
console.log('The active index is ' + slider1.activeIndex);
$scope.currentIdx = slider1.activeIndex;
console.log('The active card is ' + $scope.cardNumbers[$scope.currentIdx].CardNumber);
sharedProperties.setProperty($scope.cardNumbers[$scope.currentIdx].CardNumber);
}
};
$scope.options2 = {
direction: 'vertical',
slidesPerView: '1',
pagination: false,
initialSlide: 1,
showNavButtons: false
};
how can i make it pass through the $http.get code block first?
i noticed i was loading angularjs more than once, so i got rid of the extra code calling angularjs in my index.html and it worked.

How to show multiple records fetched from a table in parse.com

I am using Parse for my app. I have two tables StudentDetail and Subject,as user enters his name, the id is queried from studentDetail table. and using that id i want to fetch rest of the details of Subject table.Data fetched through this code is correct but in $scope.subs i.e 19th line it overrides the data and returns only the last record,i want to store all the records in $scope.subs object iteratively.
$scope.subjectFetch= function(form,form1) {
var query = new Parse.Query(StudentDetail);
var querySub = new Parse.Query(Subject);
query.equalTo("Firstname",form1.Firstname);
query.find({
success: function(results) {
stdId = results[0].id;
querySub.equalTo("StudentId",stdId);
querySub.find({
success: function(subjects) {
alert("Success");
for (var i = 0; i < subjects.length; i++) {
var object = subjects[i];
subname = object.get('Name');
credits = object.get('credits');
code =object.get('code');
duration = object.get('duration');
alert("Subject name: "+subname+"\n Credits :"+credits+"\n Code :"+code+"\n Duration:"+duration);
$scope.subs=[{Name:subname,credits:credits,code:code,duration:duration},{Name:"xyz",credits:5}];
//console.log($scope.subs);
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
You are setting $scope.subs to be a new array with a single object in it inside your loop, which is why only the last one is there when it is done.
Try the following success handler instead:
success: function(subjects) {
// reset subs array
$scope.subs = [];
for (var i in subjects) {
var subject = subjects[i];
// push new object into the array
$scope.subs.push({
Name: subject.get('Name'),
credits: subject.get('credits'),
code: subject.get('code'),
duration: subject.get('duration')
});
}
// log the populated array
console.log($scope.subs);
Alternatively if you use something like the Underscore library, you can just map the items:
// replace success function body with this:
$scope.subs = _.map(subjects, function(subject) {
return {
Name: subject.get('Name'),
credits: subject.get('credits'),
code: subject.get('code'),
duration: subject.get('duration')
};
});
console.log($scope.subs);

Resources