ParseJS - .get() not working for object inside object - javascript

I'm using ParseJS to try and get the data from a user (from another object) and no matter what I try it doesn't seem to be working, i thought it would be as simple as doing:
var message = results[i].get('Messager').get('username');
To keep in mind:
var message = results[i].get('Messager'); // gives me message object perfectly fine
var message = results[i].get('Messager').id; // gives me user id perfectly fine
Since without .get('username') it works fine i'm unsure why it's not working. I've used the console to inspect that object and the data i want is stored normally inside the object, even when i do:
console.log(results[i].get('Messager'));
and then save as a temp variable inside the Chrome console then use:
temp1.get('username');
It works fine, this is really bothering me because i dislike the workaround.
What am i doing wrong? Why is doing .get('Messager').get('username'); not working?
There are no spelling/capital mistakes in what I'm doing
Here is an image with the .message object when i do console.log(results[i].get('Messager');
After further testing I found that the 'Messaging' object works but 'Messager' object doesn't work? The objects look the exact same..

Related

Retrieving data objects from localstorage and reload them into array

I have taken some key points from this site already when it comes to transferring data to and from local storage. However, it does not seem to work properly. In short, I am trying to load data in the form of an array containing objects, to and from the local storage. I am aware that it is saved as a string within local storage and have then used JSON formatting to solve that problem, it is also where I think the problem might be when there is objects within objects. However, in its current state, it does not seem to work. Any ideas?
var students = [{name: "Petrina", age: "20"}];
function saveList(){
localStorage.setItem('somekey', JSON.stringify(students));
console.log("Saved to Local");
}
function loadList(){
students = JSON.parse(localStorage.getItem('somekey'));
}
The code gives no errors, I am using the functions in relation to loading the window.
window.onload = () => { loadList() }
You've added that you are calling from onload, and,
in your code you are loading into students.
add to the beginning of your code:
var students;.
Note: Some Objects need special handling.
For example: Date:
var date = new Date();
localStorage.date = JSON.stringify(date);
date = new Date(JSON.parse(localStorage.date));
Answer before additional information:
key is a method of localStorage.
Don't use it.
Use students instead, for example.
Storage.key()
(This is assuming that you call the functions)
var students = [{name: "Petrina", age: "20"}];
function saveToLocalStorage(key, value){
localStorage.setItem(key, JSON.stringify(value));
console.log("Saved to LocalStorage");
}
function loadFromLocalStorage(key){
console.log("Loaded from LocalStorage");
return JSON.parse(localStorage.getItem(key));
}
console.log("students: "+students);
saveToLocalStorage("stu", students);
var st2=loadFromLocalStorage("stu");
console.log("st2: "+st2);
cannot be run in a snippet: sandboxed, no access to localStorage - cross origin.
It got solved! - I am unsure what the problem was. I cleared all the calls for all functions and debugged the save and load function 1 at a time whilst watching the local storage data. The problem to begin with was that it simply did not save the data that got updated during runtime, hence it just kept loading values that always were the same. #iAmOren's idea by creating a function that returns a value might have done it, however I am unsure why. Thanks for the responses & Support!
Are you missing a call to your saveList function?
Calling saveList before calling loadList works as you expect.
Please refer: https://codesandbox.io/s/strange-brown-3mmeq?file=/index.html

Angular json marshalling via $http

I am having a very weird problem. Using $http my response is not getting marshalled correctly into map/object. The data is a map of lists. The issue is that the element 100110150000000751 has a value from the raw data in the first log "1 group data[". but after its converted to json it has the value of an empty array. You can see the image of the "after conversion" log. This all works fine in chrome with debugger open but not if debugger is closed. The method is so simple and there is little to no room for race conditions or anything. I created a little desktop html file with all the same libraries and just calling a function without $http and it works without issue. Angular 1.4.5 Any help is much appreciated.
transformResponse: function(groupMapResponse){
console.log("1 group data ["+groupMapResponse+"]");
var asdfdsafsad = (typeof groupMapResponse === 'string')?JSON.parse(groupMapResponse):groupMapResponse;
console.log("after conversion:",asdfdsafsad);
return asdfdsafsad;
}
The console log is:
1 group data [{"1000110150000002801":["1000110020000007753"],"1000110150000002855":["1000110020000009470"],"1000110150000004452":["1000110020000007895"],"1000110150000004362":["1000110020000006355"],"1000110150000004361":["1000110020000010309"],"1000110150000000751":["1000110020000007950"],"1000110150000004412":["1000110020000006353"]}]
Turns out the issue was not in marshalling at all. The object was already linked to a bound object and was later altered but the log message was updated with the bound value. Anyway...

Can't iterate over array in certain pages

I have this simple array:
var a = new Array();
a.push(o);
When running this code:
a.forEach(function(o){console.log(o)});
In a blank page - I can see the object being logged to the console.
The thing is - I have this code running in a another page and
The foreach loop does not log anything, even though I can see the object in a[0].
I suspect this may has to do with the Prototype JS lib that this page utilizes- though I wasn't able to prove it.
Does that sound familiar?
Thanks.
The prototype replaces console log messages, use this code to see the actual console log:
delete console.log
After this console messages will appear.

Backbone model.save() fails unless evaluated in DevTools console first

I have this function in a Backbone view:
updateToServer: function(e) {
e.preventDefault();
var id = e.target.getAttribute('data-id');
var file = this.collection.get(id);
var data = {};
$(e.target).serializeArray().map(function(x) {data[x.name] = x.value;});
file.save(data);
this.$el.modal('hide');
}
If I allow this to run naturally, I get undefined is not a function on file.save(data). However, if I set a breakpoint in Chrome DevTools at file.save(data) and evaluate that function manually in the console before resuming, both save functions work.
Why is this happening and how can I fix it?
Here's the entire view in case you need additional context: https://gist.github.com/raddevon/d3ddf1bba101b6b67c4b#file-supportfilesview-js-L155-L163
Update: New discovery: On the second run, this works. I have an even listener on form submit. When I click the submit button the first time, I get the error. If I click again, the model saves.
Can you try this
updateToServer: function(e) {
e.preventDefault();
var id = e.target.getAttribute('data-id');
var file = this.collection.get(id);
var data = {};
$(e.target).serializeArray().map(function(x) {data[x.name] = x.value;});
this.$el.modal('hide');
setTimeout(function(){
file.save(data);
}, 200); //try with different values for timer
}
I have added a 200 millisecond timer.
This might not be your actual solution but at least you will come to know if there is some asynchronous stuff going on before 'file' is actually formed.
Try different values for the timer. I mean keep increasing the timer and see if you are still not able to get rid of the error.
Once you are sure that 'file' is formed asynchronously then you can look into why that's happening.
And try console.logs instead of debuggers for debugging so that you can test without pausing the execution.
Hope that helps.
This was not at all what I suspected, and I hadn't given enough information in the question without realizing it. The line in my code that triggered the exception was file.save(), but the actual exception was happening inside Backgrid.
I provide a form to allow users to update models from the collection displayed in a grid. A particular column is defined as an integer column, but I hadn't converted the value coming from the form to an integer. As a result, Backgrid was trying to run toFixed on a string. I modified my form serialization code to convert strings containing only integers into integers. Now, everything works as expected.
Here's that serialization code:
$(e.target).serializeArray().map(function(x) {
data[x.name] = x.value === 'on' ? true : x.value;
if (!isNaN(parseInt(data[x.name])) && isFinite(data[x.name])) {
data[x.name] = parseInt(data[x.name]);
}
});
If I had to guess, I'd say that's probably a bit naive, but it seems to be working well in my application.
Thanks to everyone for the help!

Appending data to an element works but then I can't retrieve it

I'm attaching response data to the target of a jquery plugin when a preload option is set. It all seems to work a charm except the data I set is inaccessible. When I log $(elm).data(); I can see that the object was returned with the appropriate data set to the appropriate key. But when I try console.log($(elm).data('key')); I get undefined. I also get undefined when I try var elmData = $(elm).data(); console.dir(elmData.key);. So I'm logging the object on one line, seeing it in the console, trying to access a property I just confirmed exists and getting undefined.
Here is my function:
this.preloadData = function(folders)
{
var getString;
for(var folder in folders ){
getString = 'folder='+folders[folder]+'&uri='+uri+'&thumbSide='+options.thumbSide;
$.get(options.handler, getString,
function(response, serverStat, xhr)
{
$self.data(folder, response);
});
}//for
var $selfData = $self.data();
console.log($selfData);//Object{ editorial : "data", testingdata : 'some meaningless words'}
console.log($self.data());//Object{ editorial : "data", testingdata : 'some meaningless words'}
console.log($self.data.editorial);//undefined
console.dir($self.data('editorial'));//undefined
console.log($selfData['editorial']);//undefined
console.log($selfData.editorial);//undefined
$self.data('testingdata', 'some meaningless words');
console.log($self.data());//Object{ editorial : "data", testingdata : 'some meaningless words'}
console.log($self.data('testingdata'));//'some meaningless words'
}
I know the namespacing is too simple, I just tried to cut out as many factors as I could to try and understand why this isn't working. I'm developing in chrome but I've tried it in firefox also and get the same.
-----------------------------EDIT------------------------------------------
I understand what the problem is now. The console reflects all changes to an object that get made regardless of if they were made when the object was logged. So when I logged the object the property didn't exist yet because the response had yet to come back from the server, but the log still shows the property as existing because at some later point in the script execution it DID exist. Because primitive values aren't logged in the same way, the call to log the property shows up as undefined because it WAS undefined. If I refer to the property some seconds later, say on a click event it is defined(which is how I intended it to work anyway). I just got really caught up in debugging this function before I actually implemented it.
Can you do this?
$self.data['editorial']
try
console.log($self.data.editorial);

Categories

Resources