Ad

Cloud Functions Using Cloud Firestore And TypeScript Don't Execute

Edit: after diving into the guides and following Doug's answer I tried to edit my code but I still can't make it to work. This is my new code (index.ts):

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

export const createUser = functions.auth.user().onCreate((user)=>{
const newUser = new MyUser(user.uid, "Friend","", new Array, Array("en"),0,0,0 )
return db.doc("users/"+user.uid).set({newUser});
});

class MyUser{
    uid: String;
    first_name: String;
    last_name: String;
    communities_list: Array<string>;
    lang_list: Array<string>;
    reputation: Number;
    join_date: Number;
    last_activity: Number;
    constructor(uid:string, first_name:string, last_name:string, communities_list:Array<string>, lang_list:Array<string>, reputation:Number, join_date:Number, last_activity:Number) { 
      this.uid = uid;
      this.first_name = first_name; 
      this.last_name = last_name; 
      this.communities_list = communities_list; 
      this.lang_list = lang_list; 
      this.reputation = reputation; 
      this.join_date = join_date; 
      this.last_activity = last_activity; 
   }  
}

I am new to both TypeScript and Cloud Functions. The two test I am trying to create are one triggered by an auth trigger and another triggered by a creation of a new document. The result for both operations should be a write to the data base, but in neither a write actually happens.

Can someone see what the problem in my code is? Also, can you debug cloud functions? How can I know where my code failed?

These are my functions:

import * as functions from 'firebase-functions';

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

const db = admin.firestore();

exports.createUser = functions.auth.user().onCreate((user)=>{

const userMap = new Map;
userMap.set("uid", user.uid);
userMap.set("first_name", "Friend");
userMap.set("last_name", "");
userMap.set("communities_list", new Array);
userMap.set("lang_list", new Array("sdsdf"));
userMap.set("reputation", 0);
userMap.set("join_date", 0);
userMap.set("last_activity", 0);

db.doc('users/'+user.uid).set({userMap});

});

exports.newIndiceAlgolia = functions.firestore.document('communities/{newCommunity}').onCreate((snap, context) => {

    const newValue = snap.data();

    if (newValue != null){
        const objectID = newValue.objectID;
        const title = newValue.title;
        const description = newValue.description;
        const members = 0;

        const map = new Map;
        map.set("objectID", objectID);
        map.set("title", title);
        map.set("description", description);
        map.set("members", members);


        db.doc('check_function/jjjj').set({map});
    }

  });



export const helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
});

The third one was just a sample one that was there when the document was first created so I left it as a check and it works just fine.

Ad

Answer

You're ignoring all the promises from all the asynchronous calls to Firestore. In order to write effective code for Cloud Functions, you will absolutely need to understand how JavaScript promises work, since Cloud Functions requires you to return a promise that resolves only after all the asynchronous work is complete in your function. If you don't do that, your work may get cut off without completing.

Minimally, in your cases, you can put a return in front of each database operation you're doing:

return db.doc('check_function/jjjj').set({map});
return db.doc('users/'+user.uid).set({userMap});

You will also find that TypeScript Map won't work for you. You will need to use a plain JavaScript object with properties in order to add data to Firestore. In TypeScript, Map is a special collection, which isn't supported by the Firebase SDK.

Ad
source: stackoverflow.com
Ad