How do I read this JSON string? - asp.net

I am getting this JSON string from an ASP.Net webservice:
{"d":{"Table":[{"col1":123,"col2":"name","col3":"name","col4":100,"col5":"\/Date(1153033200000)\/"},{"col1":123,"col2":"name","col3":"name","col4":101,"col5":"\/Date(1153033200000)\/"},{"col1":123,"col2":"name","col3":"name","col4":102,"col5":"\/Date(1153033200000)\/"}]}}
In my jQuery how do I reference the Table code so I can loop through the data?
msg.d[i].col1
What am I missing? msg.d.table[i]?

The property d is an object that contains the property Table, which is an array of objects that contain the property col1.
So, you use msg.d.Table to access the array, msg.d.Table[i] to access an item in the array, and msg.d.Table[i].col1 to access the property in the item.
Note that Javascript is case sensetive, so while msg.d.Table works, msg.d.table won't.
This gets the array and loops through it:
var tableArray = msg.d.Table;
$.each(tableArray, function(){
alert(this.col1);
});

msg.d is an object. msg.d.Table will give you what you want.
To iterate:
$.each(msg.d.Table, function(row) {
// Get specific value:
window.alert(row.col1);
// Iterate through all columns:
$.each(row, function(column, value) {
// Do something..
});
});

$.each(msg.d.Table, function(i, val) {
alert(val.col1);
});
I hope this helps!

You can use jQuery's JSON parser:
data = jQuery.parseJSON(JSON_DATA);
And then refer to objects directly via the data variable:
data.my_property

Related

How to pass the data to a template using blaze-layout render?

I tried the following code to pass data to a template and receive it in onCreated() but I cannot access the data.
deviceInfo.js:
BlazeLayout.render('layout',{main:'deviceInfo',stats:'paramstats',attr:"SOME_DATA"});
deviceInfo.html:
{{>Template.dynamic template=stats data=attr}}
paramstats.js:
Template.paramstats.onCreated( () => {
console.log("onCreated");
console.log("Data is:",this.data.attr);
});
But I get TypeError: Cannot read property 'attr' of undefined.
where am I going wrong?
You need to use the normal function syntax for onCreated callback. Arrow function will bind the context of your function to the outer scope automatically, it is the cause of your problem. Try this:
Template.paramstats.onCreated(function() {
console.log("onCreated");
console.log("Data is:",this.data.attr);
});
I am using Meteor 1.4.# and I was able to retrieve the parameters like so:
BlazeLayout.render("page", {
params: ['fullscreen', 'route']
});
// page.js
Template.page.onCreated(function() {
let params = this.data.params();
console.log(params);
}
Not quite sure why you're using two levels of indirection. BlazeLayout.render() is giving you one level and then you're using a dynamic template within that? Why not directly render the template you ultimately want using BlazeLayout.render()?
In any case, you're dereferencing your data context indirectly.
In the BlazeLayout.render() call you're setting the attr variable to some value.
Then in your dynamic template you're using data=attr but this means that inside your template helpers that this is going be have the value of attr. There will be no data subkey added automatically.
You don't show the value that you're setting for attr so it's not even clear that you have an attr subkey in your attr variable, that would also be confusing to anyone else who ever tries to debug your code.
#khang is correct about not using the arrow function syntax in onCreated(), try:
Template.paramstats.onCreated(function(){
console.log("onCreated");
console.log("Data is:",this);
});
this should have whatever value you stuffed into attr in your BlazeLayout.render()

Applying Meteor collection insert in javascript array for loop

I am new to javascript and very new to Meteor. Is this code correct? I need to define a function which will take an array of values and insert them in a Meteor Collection "FooterButtons"?
Client code
replaceCollectionContents(['NO', 'B', 'YES']);
Both code
replaceCollectionContents = function (buttonsList) {
FooterButtons.remove();
for(i = 0; i < buttonsList.length; i++) {
FooterButtons.insert(buttonsList[i]);
}
};
You cannot directly insert a string to a collection. The insert method expects a document which is of type object.
Try this instead -
FooterButtons.insert({ text: buttonsList[i] });
Also, I notice that you are trying to clear your FooterButtons collection. Please note that you cannot clear a collection like this from the client side as it is considered untrusted code. From client side, you can only remove one document at a time, specified by its _id.
I would recommend you to use a method instead.
Meteor.methods({
replaceCollectionContents: function (buttonsList) {
// remove all existing documents in the collection
FooterButtons.remove({});
// insert new button documents into the collection
buttonsList.forEach(function (button) {
FooterButtons.insert({ text: button });
});
}
});
And call this method when needed
Meteor.call("replaceCollectionContents", ['NO', 'B', 'YES']);
Inside the method, note that I'm passing a {} selector to the remove method because for safety reasons, Meteor does not remove any documents if selector is omitted.
You can read more about remove in Meteor docs.
If I understand correctly, you need to seed data into FooterButtons collection, correct?
Put this code somewhere on your server folder:
buttonsList = ['NO', 'B', 'YES'];
if (FooterButtons.find().count() === 0) {
_.each(buttonsList, function(button) {
FooterButtons.insert({text: button});
});
}
Run meteor and check your mongodb FooterButtons collection. Let me know. I'll make explanation if this work
If you need to update, then update it:
FooterButtons.update({text:'B'}, {$set:{text:'Extra'}});

Iterating over $firebaseArray and $firebaseObject

I am trying to iterate over firebaseObject and firebaseArray fetched from my Firebase but they don't seem like normal javascript objects and arrays.
My data is stored in the following form
'mainKey': {
'key1':'value1',
'key2':'value2'
},
'mainkey2': {
'key3':'value3'
}
I've tried the following code
var firebaseRef = new Firebase("https://<my-app>.firebaseio.com/);
var fbArray = $firebaseArray(firebaseRef);
var fbObject = $firebaseObject(firebaseRef);
for(var i=0;i<fbArray.length;i++){
console.log(fbArray[i]);
}
console.log(fbObject);
console.log(fbObject.mainkey);
console.log(fbArray.length);
This gives the following output in the console
Object { $$conf={...}, $id="test", $priority=null, more...}
undefined
0
Though the object returned has mainkey property but I'm not able to access it.Why does this happen? And how should I iterate over them ?
You could try for..in loop to iterate over an object.
Below is an example:
for (var key in fbObject) {
console.log(fbObject[key]); // You could use this method with all objects in JS
}
here's the info you need to know
The $firebaseArray service takes a Firebase reference or Firebase Query and
returns a JavaScript array which contains the data at the provided Firebase
reference. Note that the data will not be available immediately since
retrieving it is an asynchronous operation.
You can use the $loaded() promise to get notified when the data has loaded.
https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray
fbArray.$loaded(function() {
//here you can iterate over your object
});

Why Does Firebase orderByChild() return undefined?

I've got a Firebase with a simple bit of data:
There's a list of "players", each with a self-generated GUID, and each containing a value "Count". At my request (e.g. using once()), I want to be able to query the players sorted by the Count value. So, based on the Firebase documentation, I'm using orderByChild(), but it always comes up as undefined when I run the code:
var fb = new Firebase("https://morewhitepixels.firebaseio.com/");
fb.child("players").orderByChild("Count").once("value",function(data) {
// do something with data
});
But this code always returns Uncaught TypeError: undefined is not a function pointing to that second line of code.
What am I missing?
I'm not sure what you do inside the callback, but this works fine:
fb.child("players").orderByChild("Count").once("value",function(data) {
console.log(data.val());
});
Keep in mind that the data parameter is not the actual data yet. It's a DataSnapshot on which you have to call val() first.
You'll probably want to loop through the children, which you can do like this:
fb.child("players").orderByChild("Count").once("value",function(data) {
data.forEach(function(snapshot) {
console.log(snapshot.val().Count);
});
});
The above example prints out all your children in the order you requested:
120320
181425
185227
202488
202488
202488
202488
245197
245197
487320
Alternatively you can use on('child_added' instead:
fb.child("players").orderByChild("Count").on("child_added",function(snapshot) {
console.log(snapshot.val().Count);
});

How to get the object key when using value in angularFire

I have below code to query the index from users node then pass the index to todos node to get the data I want but it only watching when a child is added so when a child is removed in the index it doesn't update the view.
I have no idea how to get the object key if I use value in the index node.
Any idea? or any better way to query mastered data using angularFire?
FirebaseRef.$child(user.uid).$on('child_added', function(a) {
FirebaseRef.$child('todos/' + a.snapshot.name).$on('value', function(b) {
$scope.todoList[a.snapshot.name] = b.snapshot;
});
});
try the following (not tested).
FirebaseRef.$child(user.uid).$on('child_added', function(a) {
FirebaseRef.$child('todos/' + a.name()).$on('value', function(b) {
$scope.todoList[a.name()] = b.snapshot;
});
});
You shouldn't need 'snapshot' in your reference eg a.snapshot.name, you should just reference the object returned in your callback function

Resources