I'm trying to use Restangular to chain calls to one(), and haven't figured out why this isn't working:
my_data = Restangular.one('mydata', 12345).one('foo', 789).get().then (some_data) ->
$scope.data_numbers = some_data.data.numbers
This should generate a call to '/mydata/12345/foo/789' but the chaining doesn't work. Instead, I get the error "Undefined is not a function". When I set a breakpoint and look at the result of doing just:
my_data = Restangular.one('mydata', 12345)
I can see that singleOne is undefined:
my_data
> Object {id: 197330, singleOne: undefined, route: "revenue", getRestangularUrl: function, getRequestedUrl: function…}
That seems to be the problem, though that's a bit of a guess. According to the Restangular docs and my previous experience, all of this should work, so I'm puzzled. I'm sure I'm missing something obvious here. To keep from being blocked I'm using a temporary customGET() but that's not very nice, so... Any suggestions would be very much appreciated.
Related
I ran into a strange situation today, thanks to Javascript. I have a Object that look something like this.
$scope.main = [{main : 1},service:true];
Now when I try to expect this inside the jasmine test case for equating the Objects :
expect($scope.main).toEqual([{main : 1},service:true]);
This gives me an error :
Unexpected Token.
Strangely, This is a valid object for Javascript. But Jasmine is not able to accept that.
Is there any way to test this?
Thanks in advance!
EDIT : Attaching a structure screenshot.
Update
I see now based on your screenshot that you are creating the main object in multiple steps. I've shortened it to the following:
var main = [{main: 1}];
main.service = true;
In dev-tools, you are seeing main as something that looks like this: [{main: 1}, service: true].
However, don't be mislead. Dev-tools is showing you a structure that is just meant to be informative. You can't actually create that structure in one line of javascript, because it is invalid. You have to create it in multiple steps, like you have.
This is why when you try to create it in your test in one line, you are getting an Unexpected Token. error. In your test, you have to create the expected object in a similar fashion to how you created your main object. For example:
var expected = [{main: 1}];
expected.service = true;
expect(main).toEqual(expected);
I need to assign a variable to a string and pass that variable to a Firebase query in a $firebaseObject. But, when I try this, the firebaseObject is null. I tried with directly putting the string in and it works, but I don't want that.
Code:
//var itemkey='-JyO7Zsgsucf5ESttJwt';
var itemkey=window.localStorage['fbid'];-------> string: '-JyO7Zsgsucf5ESttJwt'
console.log(typeof itemkey); ------------------> string
$scope.galphoto = $firebaseObject(ref.child(itemkey));
console.log($scope.galphoto) ------------------> null
When I call
$scope.galphoto = $firebaseObject(ref.child(itemkey));
With itemkey='-JyO7Zsgsucf5ESttJwt',
then console.log($scope.galphoto) is properly shown. However, when I use what I really want to use,
window.localStorage['fbid']
then console.log($scope.galphoto) is null.
Why?
I think you may be trying to console.log the result before it's available. $firebaseObject returns the data with some delay. You may try to do something like
$scope.galphoto = $firebaseObject(ref.child(itemkey));
$scope.galphoto.$loaded().then(function () {
console.log($scope.galphoto);
}
You should see the expected result, since you're waiting for the promise to respond. It could help you better understand how to get the desired result.
Okay, I've been bashing my head bloody on this one:
I have the following JSON coming back from the server:
{
"SendDate" : "2015-03-16T22:48:27.747",
"SendTo" : {
"ContactIds" : ["28a24538-cdfc-4453-920d-86f57d7eaf22"],
"GroupIds" : []
},
"Message" : "MEETING TIME!!!!!"
}
I have checked this with several REST clients - this IS what comes back.
I have AngularJS "getting" this with an $http.get() operation, but I get an undefined on the "ContactIds" value - so, what I see in the JS Console is:
SendDate : "2015-03-16T22:48:27.747"
SendTo:
ContactIds: Array[1]
0: undefined
length: 1
I have NO IDEA what can be causing this.
Any ideas?
UPDATE:
I have attached an interceptor and intercepted the response and the result is the same when I feed the data to the console - but when I use:
JSON.stringify(data)
I can see that the Data in the Array is THERE!
UPDATE 2:
Okay now this is driving me nuts. I have played with the interceptor and if I stringify the response and then use JSON.parse() - it works fine, but when I pass the response through, it comes out messed up again.
I traced it through angular's parsing process all the way to the "fromJson()" function. (code below:) It comes into the function as a string. (Now here's the Bizzarro part)
I altered the code like this:
function fromJson(json) {
var obj1 = JSON.parse(json);
console.log("Obj1:");
console.log(obj1);
//my altered angular code
var obj2 = isString(json) ? JSON.parse(json) : json;
console.log("Obj2:");
console.log(obj2);
// Pass to program...
return obj1;
//return obj2;
/* original angular code:
return isString(json)
? JSON.parse(json)
: json;
*/
}
If I run it and return obj1, the console logs obj1's ContactIds "0" index as "undefined" - but obj2 logs as "28a24538-cdfc-4453-920d-86f57d7eaf22".
"GREAT!", I'm thinking - so I return obj2, but now it logs undefined but obj1's "0" index is now the correct value. (WTH?)
So I reverse the code, just to see, and Return obj1 - and I'll be damned - obj2 returns "28a24538-cdfc-4453-920d-86f57d7eaf22" and obj1 is undefined. (It's like teasing a monkey.)
It HAS to be something later on in the pipeline that is doing it - OR - it may have something to do with the array being GUID strings - but I use GUID strings elsewhere with no problems.
It could also be another "angular process" that I'm unaware of that is causing this - angular is quite impressive.
Either way, I'm super-confused.
This is so stupid - I'm surprised that an array of strings is such a difficulty - and what's worse, it seems I'm the only one having this problem. (I researched this for six hours yesterday...)
Any other ideas, guys?
OH MY GOD I'M SO STUPID!!!
First I'd like to thank everyone for trying to help me.
The answer is this - there was no problem, that is, not with Angular or its parser.
The Problem is that the Console was logging transformed data, which had an error at MY controller. The data on MY end was not matching the data in the list that I had in my controller - (Database Error).
BOTTOM LINE:
If you have this error pop up, do NOT troubleshoot from the top-down - troubleshoot from the bottom-up.
Angular has many checks and balances - if you don't get "bleeding failures" throughout the program, chances are the error is in YOUR code.
Thank you everyone for your help.
I'm using AngularJS and Firebase and AngularFire.
The following code is giving me trouble:
images.$add(imageUpload).then(function(ref) {
console.log(ref);
});
I've used similar code elsewhere, which works just fine, but here I get objects that look like this returned:
H {n: be, path: J, Ca: undefined, da: undefined, va: undefined…}
Aa: undefined
Ca: undefined
Ra: undefined
da: undefined
n: be
path: J
va: undefined
__proto__: c
I've tried messing around to discover what I'm doing wrong, but with no luck. I'm assuming I'm doing something relatively simple wrong?
I've fiddled around with it and discovered that they key of the newly created data, which is what I'm looking for, is actually stored under path/m. So it seems like the data returned is simply just being formatted oddly?
By using:
console.log(ref.path.m[1]);
I can get what I would have expected to find calling
console.log(ref.name);
Any help is much appreciated.
Name is a function and not a key in the data. So to get the id back you must call:
console.log(ref.name());
rather than:
console.log(ref.name);
I am attempting to retrieve an entry from my datastore with this:
query = UserData.gql("WHERE mDeviceId = :1", id)
Utils.log("my Object:" + str(query))
entry = query.get()
It just so happens that the variable 'id' doesn't even exist (typo), so I know how to fix this, but I don't understand why the result I get won't let me call get() on it. When I do, I get the error:
Exception: Unsupported type for property : <type 'builtin_function_or_method'>
Normally I just check if entry == None to see if I get no results. Does anyone know why this occurs and if I should be doing my checks for None differently, in case I have such typos in the future?
A variable named id is not defined in your code, so it passes the builtin function id, and QL complains that it's getting a function when it expected a value (integer?).
Check to make sure you're assigning id a value before you use it.
Even better, don't shadow builtins with your own variables-- it'll cause confusing errors like this. :-)