Using .numChildren() in AngularFire - firebase

I'm trying to use .numChildren() in AngularFire, but not sure I'm doing it correctly.
function getServiceProviders(serviceId) {
var serviceProviders = ref.child('services').child(serviceId).child('providers');
return serviceProviders.numChildren();
}
I'm getting the following error:
TypeError: e.numChildren is not a function
Not sure if this is due to me using Browserify, or I'm just trying to access numChildren incorrectly.
Any help is appreciated. Thanks in advance!

Your code snippet doesn't use AngularFire, it only uses the Firebase JavaScript SDK. Although your project undoubtedly uses AngularFire, it doesn't relate to this question.
When you look at the documentation for the .child() method in the Firebase JavaScript SDK, you'll see that it returns a Firebase reference. And if you look further into that class, you should notice that it doesn't have a numChildren method.
numChildren is only available on a DataSnapshot object, which you get in any of the on(... event handlers.
So:
serviceProviders.on('value', function(snapshot) {
console.log(snapshot.numChildren());
}
Since the snapshot will be loaded asynchronously, you cannot return the number of children from getServiceProviders. See my answer to this question for a broader explanation of that: Asynchronous access to an array in Firebase

Related

documentReference.data(as: ) not available anymore in Firestore?

I'm trying to access the documentReference.data(as: ) but it seems like it wouldn't show on my end. I was wondering if this method is deprecated? if so is there any alternative method I can use to map a document reference to a Swift type.
Here's a screenshot of my code:
documentReference.data(as: ) not showing
What I'm try to do is something like this.
func fetchOccupants() async throws -> [Occupant] {
let snapshot = try await occupantsRef.getDocuments()
return snapshot.documents.compactMap { document in
try? document.data(as: Occupants.self)
}
}
I figured it out.
Apparently, at some point this method was moved to a separate module FirebaseFirestoreSwift (or maybe it was always there, I might be wrong).
Import this module and the method data(as:with:decoder:) becomes available:
import FirebaseFirestoreSwift
It's also mentioned on the documentation page about Codable: Map Cloud Firestore data with Swift Codable.
After a few days of testing and reading some documentation I finally solved it. It seems like using Cocoapods to add FirebaseFirestoreSwift is not working, so I tried using Swift Package Manager instead since it's more updated Package Manager for Swift.
Following this method works for me link

Running WebAssembly on Google Apps Script

I am attempting to run WebAssembly on the new V8 Google Apps Script runtime, and it appears to be supported, however it seems that async functions are terminated after they return a Promise.
let wasm= new Uint8Array([/* snip */]).buffer
function add(a,b) {
return((async()=>{
console.log("running function...")
results=await WebAssembly.instantiate(wasm)
return results.instance.exports.add(a,b)
})());
}
function test(){
add(2,3).then(console.log).catch(console.error)
}
when I run test "running function..." is logged, then nothing. No errors, no results. I have confirmed that WebAssembly.instantiate returns a Promise.
Does anyone know what is going on, or is this something to ask Google about?
Update:
Created a issue at https://issuetracker.google.com/issues/153828715
Asynchronous functionalities don't seem to be fully supported in V8 yet. There is actually an open Issue Tracker regarding this. You can click the star on the top left of the page to keep track of this issue.
In any case, please be aware that there is no explicit statement in the official documentation referring to the availability of these functionalities in V8. It just states that you can use keywords like async in your code, but it doesn't mention what functionality you will get if you use that.
Reference:
Issue Tracker: Async with V8 is not implemented as async / concurrent; documentation could be improved
V8 Runtime Overview: Improved function detection
Refactored the sample script you provided on issue tracker and got it to work from the GAS editor (maybe Google changed something since you've posted this issue). GAS is synchronous but you can still use async/await as follows:
async function testWasm() {
let bytes = new Uint8Array([0,97,115,109,1,0,0,0,1,7,1,96,2,127,127,1,127,3,2,1,0,7,7,1,3,97,100,100,0,0,10,9,1,7,0,32,0,32,1,106,11,0,28,4,110,97,109,101,1,6,1,0,3,97,100,100,2,13,1,0,2,0,3,108,104,115,1,3,114,104,115]);
let {
instance: {
exports: {
add
}
}
} = await WebAssembly.instantiate(bytes);
console.log(add(2,3));
}

Could not invoke RNFirebaseFirestore.documentSet

Hi guys I add this error and I don't understand why... I'm using the starter project of react-native-firebase, and I'm trying to use firestore
Although there might be an issue with the library which needs investigating, your code is generally wrong. State in a React component should be data which when changed, causes a re-render. You do not need to assign your collection to state, this can be done as a class property. Also, using a constructor & componentWillMount is wrong as they're essentially the same thing - I don't know enough how it'd handle both cases internally though.
Your code would better work like so:
constructor() {
super();
this.ref = firebase.firestore().collection('users');
}
componentDidMount() {
this.ref.add({ name: 'moo' });
}
I have got same error. Please create the collection with the name 'users' and add a dummy document. Then try.

angularFire $asObject doesn't push

I am new to firebase and trying to use the $asObject as in the angulerFire doc. Basically, I have profile as follows below. I use $asObject to update the email. However when I use $save(), it replaces the entire profile with only the email, rather than pushing it to the end of list ie it works like set() rather than push(). Is how it is meant to work? how do I only push?
Object
{profiles:
{peterpan:
{name:"Peter Trudy", dob:"7th March"}
}
}
My click function:
$scope.angularObject = function(){
var syncProfile = $firebase(ref.child("profiles").child("peterpan"));
var profileObject = syncProfile.$asObject();
profileObject.email= "peter#peterpan.com";
profileObject.$save();
};
You're looking for $update:
syncProfile.$update({ "email": "peter#peterpan.com" });
Note that $update is only available on $firebase and not on the FirebaseObject that you get back from $asObject. The reason for this is that $asObject is really meant as an object that is bound directly to an angular scope. You're not expected to perform updates to it in your own code.
By the way: if the rest of your code is using AngularFire in a similar way, you might consider dropping AngularFire and using Firebase's JavaScript SDK directly. It is much simpler to use, since it doesn't need to mediate between Firebase and Angular's way of working.

Can the callback parameter in FirebaseAuthClient take a context parameter?

I see that the on method of a Firebase ref can take a context parameter that is this within the context of the callback. This is incredibly useful. I am wondering -- and hoping -- that the callback function that is provided to FirebaseAuthClient can also take a callback, but my intense scrutiny of the Examples, Getting Started and SDK documentation reveal no mention of it. I attempted to scour the minified firebase-auth-client.js but stopped with my sanity still intact.
A related question: FirebaseAuthClient does not seem to be included in the JavaScript SDK area. Is it anywhere?
[Engineer at Firebase]
The Firebase Simple Login constructor now accepts a context argument as its third argument. For example, to have your Simple Login callback invoked with myContextObj as its context:
var ref = new Firebase('https://<my-firebase>.firebaseio.com');
var simpleLogin = new FirebaseSimpleLogin(ref, function(error, user) { ... }, myContextObj);

Resources