身份验证触发的云功能(创建用户)未执行 [英] Auth triggered cloud function (create user) doesn't execute

查看:44
本文介绍了身份验证触发的云功能(创建用户)未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法理解我在做什么错. 我已经成功部署了这些功能,并且可以在项目的仪表板上看到它们,但是在对新用户进行身份验证时,该功能不会执行.

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.

当我注册一个新用户(我一直在使用google登录)时,数据库中没有任何反应.但是,当我转到身份验证"选项卡时,我可以看到一个新用户已通过身份验证.

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.

这是我的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; 
   }  
}

推荐答案

我的问题是我尝试设置的对象.挖掘完它(云功能的新增功能)后,我在云功能中看到了以下消息:

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

对此

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

现在可以使用了.

这篇关于身份验证触发的云功能(创建用户)未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆