AngularFire httpsCallable Object(...) is not a function - firebase

I want to call a httpsCallable function in my Ionic 3 app. I am attempting to follow these docs: https://firebase.google.com/docs/functions/callable
I tried:
const newGame = (firebase as any).functions().httpsCallable('findCreateGame');
newGame({}).then(function(result) {
console.log("In here.");
});
Which resulted in:
ERROR TypeError: WEBPACK_IMPORTED_MODULE_5_firebase_functions.functions is not a function
I also tried the newly implemented wrapper in angularfire:
const newGame = this.afFunctions.httpsCallable('findCreateGame');
newGame({}).then(function(result) {
console.log("In here.");
});
ERROR TypeError: Object(...) is not a function
Does anyone have any experience with this yet? Here is the pull request to add the functionality if that helps at all. https://github.com/angular/angularfire2/pull/1532
EDIT---
This code actually calls the Cloud function even though it throws the same 'Not a function' error:
const newGame = this.afFunctions.httpsCallable('findCreateGame');
newGame();
I'm not sure how else to call the function, even though newGame is an object and not a function reference.

The Object(...) is not a function is thrown because you're running rxjs 5, rather than 6.
If you upgrade, the function will perform as expected.
See the rxjs migration doc for more details on the changes between 5 and 6.

In your first example make sure you are importing import 'firebase/functions' in the ts file you're calling the function.

Related

Firebase Functions unable to run HelloWorld example with Unity

I try to call the most basic https function from unity, but I always get an internal error.
I deployed the function and it works when called by the browser:
https://us-central1-infinite-space-rpg.cloudfunctions.net/helloWorld
Node.js code:
const functions = require("firebase-functions");
exports.helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
response.send("Hello from Firebase!");
});
unity code:
FirebaseFunctions functions = FirebaseFunctions.DefaultInstance;
var func = functions.GetHttpsCallable("helloWorld");
func.CallAsync().ContinueWithOnMainThread((response) =>
{
Debug.Log("result: " + response.Result.Data.ToString());
});
This is the faulted error response I get:
In the firebase dashboard I can see that the function gets called and it also completes without an error.
So the setup should be fine, but the "one or more errors occurred(INTERNAL) does not give any hint on what might be the issue.
Permissions should also be fine, all users can invoke the function.
I really hope that someone has an idea!
also tried the exact sample from firebase with the same error.
https://firebase.google.com/docs/functions/get-started
I`m now stuck with this since 2 days, is there a better place to ask such a specific question, maybe a firebase support forum?

Firebase Functions Accessing Firestore error: Could not load the default credentials

I've been attempting to get to the bottom of issues with a Firebase function I'm using to update some aggregate data in Firestore. I set up a simple test bed and found that any attempt to access the data triggers the error:
> Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
> at GoogleAuth.getApplicationDefaultAsync (/Users/michael/Documents/htdocs/vue/mjf20/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
I've attempted to just copy the entire Firebase config into the initializeApp() function, but it still generates the same error. Here's the entire test code:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.postCache = functions.https.onRequest((request, response) => {
console.log("Starting test function");
db.collection('myCollection').doc('myDoc').get()
.then(snap => {
if (!snap.exists) {
console.log('Document not found');
} else {
console.log(snap.data());
response.send(snap.data());
}
})
.catch(error => {
console.log('In catch block');
console.log(error);
response.send(error);
});
});
If I take out the db stuff, the function will work fine. As soon as I add even the simplest firestore request, it generates the error. I've seen this issue asked about before, but the situations seem different, and none of the solutions seems to work. I'm stumped.
The code is perfectly fine.
I have implemented it exactly on my testing project using this tutorial.
I have added your code to index.js and in my firestore I have added collection myCollection with myDoc with just one testing field and deployed using firebase deploy.
Everything works fine. You should focus on your environment and deployment steps to figure out what is the problem. This link that you have in the error is mentioning environment variables. Maybe a case as well.
I hope it will help!

Why invoking firebase cloud function on local simulator doesn't return any output?

I wanted to invoke (or test) my cloud functions before deploying. I found this reference https://firebase.google.com/docs/functions/local-emulator.
I am using the shell to invoke and pass the test data for my function, but i always get the response as Successfully invoked function. I have few output statement inside my function using console.log(), but that never gets printed.
firebase > post_deleted({"brand" : "Gucci","category" : "FULL LOOKS","postNumImages" : 1,"posterId" : "HcK5eIKU6jUlqhcg7t10OThZ2mq2","posterName" : "Steve Nash","reverseTimestamp" : -1.511338246002482E9,"salePrice" : 5,"size" : "medium","state" : "OPEN","subCategory" : "CASUAL","timestamp" : 1511338246036},{params:{postId:'-KzXlrvMtQ4PnDMiojz8'}})
'Successfully invoked function.'
firebase >
firebase >
My function code (partial)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
console.log("before");
var gcloud_storage = require('#google-cloud/storage')({keyFilename:
`${functions.config().serviceaccount.filename}`});
exports.post_deleted = functions.database.ref('/posts/{postId}').onDelete((event) => {
console.log("inside");
let postId = event.params.postId;
let snapshot = event.data.previous;
let subCategory = snapshot.child('subCategory').val();
let db = admin.database();
let tasksArray = [];
From the doc. The Cloud Functions shell, stream logs from your functions to the terminal window where they run. They displays all output from console.log(), console.info(), console.error(), and console.warn() statements inside your functions
So, why i don't see the inside when i invoke the function? I deployed the same function, its working as expected. Sorry, if i am missing something obvious. Looking for advice. Thanks.
Okay. After spending two days during thanksgiving !! Finally, found its a bug and created an issue on the cloud-functions-emulator sdk
Later found out there was already the same issue created a week ago

Calling Google AppScript Web App from Cloud Functions for Firebase

I'm trying to get my Cloud Functions for Firebase to call a simple web app deployed using Google Apps Script. Can someone please point to any example or help figure out whats the reason for the error in my code below. Really appreciate your help.
--
I've created a simple webapp with Google Apps Script.
function doGet() {
return ContentService.createTextOutput('Hello world');
}
And I'm calling this using request-promise within my Firebase Cloud Function. I've tried to be as close to the Google Translate example given for Cloud Functions. However, I get the following error when the Cloud Function is invoked.
RequestError: Error: getaddrinfo ENOTFOUND script.google.com
script.google.com:443
Here is my Cloud Function code -
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const request = require('request-promise');
exports.makeUppercase =
functions.database.ref('/users/{userid}/logs/{logid}/mykey')
.onWrite(event => {
var url = `https://script.google.com/macros/s/.../exec`;
var retstr = request(url, {resolveWithFullResponse: true}).then(
response => {
if (response.statusCode === 200) {
const data = response.body;
return event.data.ref.parent.child('uppercase').set(data);
}
throw response.body;
});
});
Thanks in advance,
Regards
Rahul
I had the same issue and found this answer(https://stackoverflow.com/a/42775841).
Seems like calling Google Apps Script is considered external.

Tests with redux async action and axios

I have been learning testing with
mocha
chai
sinon
enzyme
now I am testing a redux async application, and I want to know if the arguments provided to some API calls are correct, how do I do that?
I'm thinking of something like axios.getCall().args to get the arguments provided for the API call and then verify if they are correct but I can't find a command that matches what I am thinking.
I found out how,
first stub the method
axios.post = sinon.stub().returns(Promise.resolve({ data: 'test' }));
then dispatch the async action
store.dispatch(updateTodos(currentState))
.then(() => {
// this line would get the call and then get the argument
let args = axios.post.getCalls()[0].args;
done();
});

Resources