Auth triggered cloud function (create user) doesn't execute

自古美人都是妖i 提交于 2019-12-11 15:52:15

问题


Can't understand what am I doing wrong. I've deployed the functions successfully and I can see them in the project's dashboard, but the function doesn't execute when a new user is authenticated.

The purpose of the function is to create a new user object in the database for this new registered user.

When I register a new user (I've been using the google signin) - nothing happens in the database. When I go to the authentication tab though, I can see a new user was authenticated.

This is my index.ts document:

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; 
   }  
}

回答1:


My problem was with the object I was trying to set. After digging a it (new to cloud functions) I saw in the cloud functions log this message:

Error: Value for argument "data" is not a valid Firestore document. Couldn't serialize object of type "MyUser" (found in field newUser). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).

After searching for a solution for this I realized I need to change this line:

return db.doc("users/"+user.uid).set({newUser});

to this

return db.doc('users/'+user.uid).set(JSON.parse(JSON.stringify(newUser)));

and now it works.



来源:https://stackoverflow.com/questions/57177029/auth-triggered-cloud-function-create-user-doesnt-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!