【问题标题】:Cloud functions using Cloud Firestore and TypeScript don't execute使用 Cloud Firestore 和 TypeScript 的云函数不执行
【发布时间】:2019-07-24 00:53:49
【问题描述】:

编辑:在深入了解指南并遵循 Doug 的回答后,我尝试编辑我的代码,但仍然无法正常工作。 这是我的新代码 (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; 
   }  
}

我是 TypeScript 和 Cloud 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!");
});

第三个只是第一次创建文档时的示例,所以我把它留作检查,它工作得很好。

【问题讨论】:

    标签: typescript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您忽略了对 Firestore 的所有异步调用的所有承诺。为了为 Cloud Functions 编写有效的代码,您绝对需要了解 JavaScript Promise 的工作原理,因为 Cloud Functions 要求您返回一个仅在函数中的所有异步工作完成后才解析的 Promise。如果你不这样做,你的工作可能会在没有完成的情况下被中断。

    至少,在您的情况下,您可以在您正在执行的每个数据库操作前面放置一个return

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

    您还会发现 TypeScript Map 不适合您。您需要使用带有属性的纯 JavaScript 对象才能将数据添加到 Firestore。在 TypeScript 中,Map 是一个特殊的集合,Firebase SDK 不支持。

    【讨论】:

    • 感谢 Doug(恭喜获得 100k)。我将了解更多有关承诺的信息,因为现在我对它们一无所知。关于使用对象而不是地图,我有点困惑,使用云函数时我在哪里定义对象?我在我的 Android 项目中设置了一个 User 对象,但我不确定在使用云函数项目时在哪里定义这个对象。是在srcindex.ts 文件下吗?
    • 我认为查看 nodejs 的 Firestore 文档中的代码示例可能会对您有所帮助。 GitHub的functions-samples repo中也有很多示例代码。
    • 我在过去几个小时内阅读并观看了您的视频,并按照他们和您的回答尝试更改我的代码,但我仍然无法使其正常工作。请介意看看我的新代码并告诉我我做错了什么?另外,我对在编写文档路径字符串时何时使用单括号或双括号感到有些困惑。看你的视频,你好像都用过。
    • 文档路径匹配中的通配符与 TypeScript 字符串中的变量插值不同。如果您对其中任何一个有疑问,请单独发布。请不要更改原始问题的性质,因为这对可能遇到相同问题的人没有帮助。
    • 我创建了一个新问题stackoverflow.com/questions/57177029/…
    【解决方案2】:

    试试

    export const newIndiceAlgolia 
    

    因为您的代码有 2 种不同的导出样式。

    exports.newIndiceAlgolia
    export const helloWorld
    

    Firebase 控制台内有一个日志选项卡。

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      相关资源
      最近更新 更多