Using $loaded to manipulate data is not a recommended practice. To
summarize, this is using a static, one-time read event to maintain a
real-time, synchronized key/value set ($firebaseObject) or collection
($firebaseArray).

$loaded() is useful for telling when initial data has been downloaded,
which is generally in itself an abused practice, and not for much else
besides printing something to console.log.

To manipulate data, it's best to use $extend and tie into the $$updated
method (for collections: $$added, $$updated, and $$removed). This is
covered in the AngularFire guide
<https://www.firebase.com/docs/web/libraries/angular/guide/extending-services.html>
.

Here's an example of using $extend to synchronized those two objects
<https://gist.github.com/katowulf/e8f35702a79eb9524e48>. It's also
perfectly fine to just synchronize the profile and the user object
separately. It's also perfectly fine to join these two nodes if they are
always read as a single unit.

The general mantra for NoSQL is to work hard during write ops to make reads
both easy and scalable.

☼, Kato


On Tue, Oct 6, 2015 at 12:29 PM, Vincent Bergeron wrote:

Hi!



Using $loaded() is not a bad practice in such. I suggest you to read the
other post from me.



https://groups.google.com/forum/#!topic/firebase-angular/yFM2BA54B3A



You cannot think any NoSql databases (Firebase, Mongo) the same way as
relational databases (MySql, SQL Server, etc)…



But your structure is ok and should be the way to go…



VB



*From:* firebase-angular@googlegroups.com [mailto:
firebase-angular@googlegroups.com] *On Behalf Of *Justin Martiniak
*Sent:* 6 octobre 2015 14:29

*To:* Firebase + AngularJS
*Subject:* Re: [Firebase + Angular] (Angular) Ionic & Firebase + Best
Practice accessing data which might not exist?



I'm starting to wonder if I am structuring my Firebase wrong and causing
to much of a headache for myself.



I was doing a typical MySQL relational setup between a user/profile.



user {

uid: 100,

profileId: 1

}



profile {

uid: 1,

userId: 100

}



So that's why I was using $loaded() at all, I realize it's a 'bad
practice' but don't know a way around it.



Anyway, setting it up like so makes me worry about Orphans in the future,
and data not being present for IDs that are (reverse orphans? lol)



---



I'll give that a shot, I was coding like that because of:




https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-loaded

As a shortcut, the resolve() / reject() methods can optionally be passed
directly into $loaded():

*var* obj = $firebaseObject(ref);
obj.$loaded(
*function*(data) {
console.log(data === obj); // true
},
*function*(error) {
console.error("Error:", error);
}
);



On Tuesday, October 6, 2015 at 12:20:17 PM UTC-6, Vincent Bergeron wrote:

Hi!



function activate() {

vm.user = Users.get(auth.uid);

vm.user.$loaded(function() {

vm.profile = Profiles.get(vm.user.profileId);

});

}





should be



function activate() {

vm.user = Users.get(auth.uid);

vm.user*.$loaded().then*(function() {

vm.profile = Profiles.get(vm.user.profileId);

});

}



Try this…



*From:* firebase...@googlegroups.com
*On Behalf Of *Justin Martiniak
*Sent:* 6 octobre 2015 14:08
*To:* Firebase + AngularJS
*Subject:* Re: [Firebase + Angular] (Angular) Ionic & Firebase + Best
Practice accessing data which might not exist?



//

// Service (Factory) for Profiles

//



(function() {

'use strict';



angular

.module('appx')

.factory('Profiles', Profiles);



Profiles.$inject = ['$firebaseObject', '$firebaseArray', 'FirebaseUrl'];



/* @ngInject */

function Profiles($firebaseObject, $firebaseArray, FirebaseUrl) {

var profilesRef = new Firebase(FirebaseUrl + 'profiles');

var profiles = $firebaseArray(profilesRef);



var service = {

all: profiles,

get: get

};

return service;



////////////////



/**

* I've tried a variety of things here, all unsuccesfully typically:

* 1) No error checking

* 2) Try/Catch

* 3) Try/Catch with if(null) checks

* 4) Try/Catch with if(null) and return {} or null even...

*/

function get(profileId) {

try {

console.log("Getting profile with id: " + profileId);

var profileRef = profilesRef.child(profileId);

return $firebaseObject(profileRef);

} catch(error) {

console.log("Error: " + error);

}

}

}

})();







//

// Controller

//

(function() {

'use strict';



angular

.module('appx')

.controller('ProfileCtrl', UserProfileCtrl);



UserProfileCtrl.$inject = ['auth', 'Users', 'Profiles'];



/* @ngInject */

function UserProfileCtrl(auth, Users, Profiles) {

var vm = this;

vm.settings = {

enableFriends: true

};

activate();



////////////////



/**

* I've tried a variety of thigns here, all unsuccesfully typically:

* 1) Simple set on $scope for user/profile.

* 2) Resolve on routes to provide user/profile.

* 3) Resolve on routes to provide user/profile or {} (which makes
ends up yielding bad data in Firebase)

* 4) Async load in Ctrl watching for $loaded.

* 5) Async load in Ctrl watching for $loaded as well as testing for
null on user/profile.

*/

function activate() {

vm.user = Users.get(auth.uid);

vm.user.$loaded(function() {

vm.profile = Profiles.get(vm.user.profileId);

});

}

}

})();



//

// Resolve on routes...

//

// I've returned just the .get()

// I've returned the .loaded()

// I've returned the .loaded OR {}

user: ['Auth', 'Users', function(Auth, Users) {

var user = Users.get(Auth.$getAuth().uid);

return user.$loaded();

}],

profile: ['Profiles', 'user', function(Profiles, user) {

var profile = Profiles.get(user.profileId);

return profile.$loaded();

}]



//

// View bindings example (nothing special, likely uneccesary)

//

<label class="item item-input">

<span class="input-label">Display Name</span>

<input type="text" ng-model="profile.userinfo.displayName">

</label>

<label class="item item-input">

<span class="input-label">First Name</span>

<input type="text" ng-model="profile.userinfo.firstName">

</label>





So it's all very simple...



The issue I have is when I attempt to get(ID) and the return is null.
None of my code handles this and shit crashes and burns in every attempt I
make.



My goal is to just have the app work gracefully, but I don't know how to
achieve this.



And when the data comes back as null things like the view wont render
properly, buttons / events wont function, etc.


On Tuesday, October 6, 2015 at 11:50:34 AM UTC-6, Vincent Bergeron wrote:

Can you post a sample code of what you're doing?





*From:* firebase...@googlegroups.com [mailto:firebase...@googlegroups.com
<firebase...@googlegroups.com>] *On Behalf Of *Justin Martiniak
*Sent:* 6 octobre 2015 13:04
*To:* Firebase + AngularJS
*Subject:* [Firebase + Angular] (Angular) Ionic & Firebase + Best
Practice accessing data which might not exist?



(Sorry if I double posted, ain't ever use google groups)



I've chosen to use Firebase as a simple means to handle authentication and
store my over-the-air data... but I'm running into an issue (occasionally).



I'm using angularFire, so whenever I access data I have the ability to get
a $firebaseObject, $firebaseArray or null.



The null part is what catches me off guard... None of the tutorials or
demos I see go through how to handle the null aspect of the request... they
just go ahead and bind the data to $scope or check if it's $loaded() all
things which wont work with null; and then this cascades into other issues
surfacing and potentially just white/empty views.



So how, or what, is the best way to access data from Firebase that might
not be there?

--
You received this message because you are subscribed to the Google Groups
"Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to firebase-angul...@googlegroups.com.
To post to this group, send email to firebase...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/firebase-angular/ff11f31a-fb75-4599-b818-04930c65d2da%40googlegroups.com
<https://groups.google.com/d/msgid/firebase-angular/ff11f31a-fb75-4599-b818-04930c65d2da%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups
"Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to firebase-angul...@googlegroups.com.
To post to this group, send email to firebase...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/firebase-angular/4dc5d0c0-04aa-491f-a5f7-0a502f9d4876%40googlegroups.com
<https://groups.google.com/d/msgid/firebase-angular/4dc5d0c0-04aa-491f-a5f7-0a502f9d4876%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups
"Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to firebase-angular+unsubscribe@googlegroups.com.
To post to this group, send email to firebase-angular@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/firebase-angular/c01da55b-d2fd-4259-8b61-80172a871247%40googlegroups.com
<https://groups.google.com/d/msgid/firebase-angular/c01da55b-d2fd-4259-8b61-80172a871247%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups
"Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to firebase-angular+unsubscribe@googlegroups.com.
To post to this group, send email to firebase-angular@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/firebase-angular/002001d1006d%245aa91240%240ffb36c0%24%40com
<https://groups.google.com/d/msgid/firebase-angular/002001d1006d%245aa91240%240ffb36c0%24%40com?utm_medium=email&utm_source=footer>
.

For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-angular+unsubscribe@googlegroups.com.
To post to this group, send email to firebase-angular@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-angular/CADypTEZ%2BUrsF3cpw69Yp6Ok3y%3D%2BsVYZiG9prij4rD6AfRNRUvA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 9 of 9 | next ›
Discussion Overview
groupfirebase-angular @
postedOct 6, '15 at 5:43p
activeOct 6, '15 at 9:33p
posts9
users3

People

Translate

site design / logo © 2021 Grokbase