标签: google-cloud-functions

在 Node 6 上构建模拟器但 Node 6 不推荐使用 Functions 时,如何在本地运行 Google Cloud Functions?

Google Cloud Functions 模拟器仅在 Node 6 上受支持:https : //github.com/GoogleCloudPlatform/cloud-functions-emulator

注意:模拟器仅支持 Node v6.xx,不支持 Node v8.xx 或 Python。”

但是,Node 6 已弃用 Google Cloud Functions:https : //cloud.google.com/functions/docs/concepts/nodejs-6-runtime

Node.js 6 运行时已被弃用。为确保您的函数位于受支持的 Node.js 版本上,请将它们迁移到 Node.js 8 或 Node.js 10。2020 年 4 月 22 日之后,将阻止使用 Node.js 6 的函数部署。在此之后继续使用 Node.js 6 的 Cloud Functions 可能会被禁用。”

我如何在本地运行函数(为 Node 8 编写)?

google-cloud-functions

0
推荐指数
1
解决办法
1653
查看次数

Firebase 函数 onCall,响应:警告,缺少 FIREBASE_CONFIG 环境变量。初始化 firebase-admin 将失败

我尝试从本地项目调用我的测试函数。

但是每次我打电话时,我都会收到 401 错误。我不知道这里有什么麻烦,在前端我有带 api 密钥的 init 应用程序,在我有 firebase 功能admin.credential.applicationDefault();我试图通过admin.credential.cer(apiConfig),但这无济于事。

我也有环境变量GOOGLE_APPLICATION_CREDENTIALS,带有我的配置路径。

依赖关系

    "firebase-admin": "~7.0.0",
    "firebase-functions": "^2.2.0",
    "firebase-functions-test": "^0.1.6",
Run Code Online (Sandbox Code Playgroud)

我的功能

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

admin.initializeApp({
    credential: admin.credential.applicationDefault(),
    databaseURL: "https://plan-list.firebaseio.com"
});

exports.createInviteUser = functions.https.onCall( (data, context)=> {

  return data;

});
Run Code Online (Sandbox Code Playgroud)

前端功能请求

 createInviteUser(email: string) {
        let inviteFunction = firebase.functions().httpsCallable('createInviteUser');

        inviteFunction(email)
            .then((result) => {
            // Read result of the Cloud Function.
           console.log(result);
        })
            .catch(err=>{
                console.log(err)
            });
    }
Run Code Online (Sandbox Code Playgroud)

另外,我怎么看我有所有必需的标题

在此处输入图片说明 这里从控制台记录

12:54:58.063 …
Run Code Online (Sandbox Code Playgroud)

firebase firebase-authentication google-cloud-functions google-cloud-firestore

0
推荐指数
1
解决办法
1944
查看次数

将 Firebase 方法转换为异步等待

我有这个 firebase 方法,它使用电子邮件和密码创建一个 firebase 用户

async register(name, email, password,type) {
    let id;
    const createUser = this.functions.httpsCallable('createUser');

    return await this.auth.createUserWithEmailAndPassword({email,password })
      .then((newUser)=>{
        id = newUser.user.uid;
        newUser.user.updateProfile({
          displayName: name
        })
      })
      .then(()=>{
        createUser({
          id:id,
          name:name,
          email:email,
          type:type
        })
      })
  }
Run Code Online (Sandbox Code Playgroud)

它还使用获取用户详细信息和用户类型的云功能将用户添加到 Firestore 集合中。

我有3个承诺(

  1. createUserWithEmail...()
  2. updateUserProfile()1
  3. createUser()

) 彼此依赖.. 如何在一个函数中使用它们?

注意functions.auth.user().onCreate()由于用户类型字段,无法使用该方法

如何在不使用 的情况下编写此方法.then()?一些用户没有出现在数据库中

javascript firebase firebase-authentication google-cloud-functions google-cloud-firestore

0
推荐指数
1
解决办法
1321
查看次数

ReferenceError:时间戳未定义

我正在尝试将 Firebase 转换TimeStamp为 JavaScript 日期。

我引用此链接将 Javascript 对象转换为 Javascript 日期, https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp

Firestore 时间戳(来自 API):

"time": {
       "_seconds": 1563620755,
        "_nanoseconds": 688000000
 }
Run Code Online (Sandbox Code Playgroud)

在保存time到 cloud firestore之前,我必须将其转换为 JavaScript Date()

所以我使用下面的代码将对象转换为日期。

let data = request.body
let tiemStamp = new Timestamp(data.time._seconds,data.time._nanoseconds)
Run Code Online (Sandbox Code Playgroud)

但我得到了以下错误,

ReferenceError:时间戳未定义

怎么解决?谢谢!

javascript node.js firebase google-cloud-functions

0
推荐指数
1
解决办法
2373
查看次数

'DocumentData | 类型的参数 undefined' 不可分配给类型为 'DocumentData' 的参数

首先,我想说我的很多代码可能很糟糕,所以我很抱歉。我的 .ts 和 .js 经验并不是让我感到非常舒服的时候(因此我无法解决这个问题)。所以我试图将文档内容从一个文档获取到另一个文档。目前是通过快照来完成的,但我确信有更快更好的方法。我的代码如下。

当我尝试将第二个集合 (redacted2) 的文档设置为快照数据时,会发生错误。正如您从控制台日志中可以看出的,我知道 snapshot.data() 具有我需要的有价值的信息,我需要完全按照我希望的方式对其进行格式化。我假设这类似于在 swift 中有一个保护语句,我可以在其中检查 null 的值并将其分配给给定的类型(但我在这里可能是错的)。

                db.collection("Redacted").doc(context.params.sensors).get()
                .then(function(querySnapshot){
                    console.log("Query Snapshot is is isssss: ", querySnapshot)
                    console.log("Query Snapshot querySnapshot.data: ", querySnapshot.data)
                    console.log("Query Snapshot querySnapshot.data(): ", querySnapshot.data())
                    console.log("Query Snapshot querySnapshot.ref: ", querySnapshot.ref)
                    console.log("Query Snapshot querySnapshot.get: ", querySnapshot.get)

                    return querySnapshot
                }).then(function(querySnapshotData) {
                    console.log("ayoooooo blooooddd", querySnapshotData)
                    db.collection("Redacted2").doc(context.params.sensors).set(querySnapshotData.data())
                        .then((alertResponse1: any) => {
                            console.log("Successfully updated firestore: ", alertResponse1)
                        })
                        .catch ((alertError1: any) => {
                            console.log("Successfully updated firestore but with error: ", alertError1)
                        })
                })
                .catch(function(error) {
                    console.log("query snapshot …
Run Code Online (Sandbox Code Playgroud)

typescript google-cloud-functions google-cloud-firestore

0
推荐指数
1
解决办法
1495
查看次数

Google Cloud Function - 超出内存限制 (2GB) - 大量数据处理

我有一个用 Python 编写的繁重数据处理脚本。脚本每次处理一项作业时,都会使用大约 500MB 的 RAM。(原因是因为脚本从一个非常大的数据库中查找历史记录。)处理脚本每行运行也需要大约 3 分钟。

我们已将 Python 脚本部署到 Google Cloud Function。当我们调用该函数同时处理三个作业时,该函数工作正常,内存使用量约为1500-1600MB;一切都是花花公子。

但是,当我们尝试调用该函数来同时处理 10 个或 100 个作业时,该函数因内存不足而被终止。我们在文档中注意到函数在任何时候的内存限制是 2GB。可以肯定地说,我们不能将其增加到 10GB、100GB 或 1000GB,以便我们可以并行运行更多的脚本实例吗?老实说,为什么每个函数是 2GB,而不是每次调用 2GB?我很想获得无服务器功能,以便在 Google 上进行繁重的数据处理工作;这似乎不可用。

如果是这样,您会说实现我们目标的最佳方法是使用具有 1000GB RAM 的标准 Google VM 吗?

谢谢。

google-cloud-platform google-cloud-functions

0
推荐指数
1
解决办法
1084
查看次数

Firebase 托管重写为云功能提示登录

我在从Firebase Hosting提供一些动态内容时遇到了一些麻烦。

我编写了一个http.onRequest()云函数,它返回一个图像(内容类型:图像/jpeg)作为其响应。如果我直接在其 url 上访问该函数,该函数将按预期工作:

https://us-central1-my-project-id.cloudfunctions.net/hosting-getPartnerImg

根据文档,我正在使用该us-central1地区。

我也希望能够使用 Firebase Hosting 调用此函数,我已对其进行了如下配置:

firebase.json(片段)

"rewrites" : [
{
    "source" : "/pimg",
    "function" : "hosting-getPartnerImage"
}
],
"headers": [ 
{
    "source": "/pimg",
    "headers": [ {
            "key": "Cache-Control",
            "value": "max-age=60"
        },
        {
            "key": "Access-Control-Allow-Origin",
            "value": "*"
        } 
    ]

}]
Run Code Online (Sandbox Code Playgroud)

index.js(片段)

"rewrites" : [
{
    "source" : "/pimg",
    "function" : "hosting-getPartnerImage"
}
],
"headers": [ 
{
    "source": "/pimg",
    "headers": [ {
            "key": "Cache-Control",
            "value": "max-age=60"
        },
        {
            "key": "Access-Control-Allow-Origin", …
Run Code Online (Sandbox Code Playgroud)

firebase firebase-hosting google-cloud-functions

0
推荐指数
1
解决办法
463
查看次数

如何防止云调度程序多次触发功能?

我每分钟都用 cloud scheduler 触发一个云功能[* * * * *]

Stackdriver 日志表明该函数似乎已被触发并在同一分钟内运行两次。这可能吗?

PubSub 承诺至少交付一次,但我认为 GCP 会自动处理调度程序 -> 函数工作流的重复触发器。

防止此功能每分钟运行一次以上的良好模式是什么?

google-cloud-pubsub google-cloud-functions google-cloud-scheduler

0
推荐指数
1
解决办法
588
查看次数

如何恢复已删除的云功能

我不小心删除了我在 CLI 上的所有云功能,还有办法恢复吗?我尝试搜索 Google Cloud Platform 功能以恢复它,但无济于事。

firebase google-cloud-functions google-cloud-firestore

0
推荐指数
1
解决办法
944
查看次数

使用 firebase 云功能验证 firebase 应用程序中是否存在电话号码

我是 Firebase(及其所有功能)领域的新手。我已经阅读了文档,并且能够正确使用 web sdk。我创建了一个文件,其中我所有当前的 firebase 代码都写在下面的 firebaseApi.js 中。另外,下面是我如何使用 registration.js 下的函数的示例(如果我做错了,请纠正),示例有效。我试图实施

 admin.auth().getUserByPhoneNumber(phoneNumber),
Run Code Online (Sandbox Code Playgroud)

我想用它来检查当前输入的电话号码是否已存在于应用程序中。但我已经阅读了 Admin SDKs 不能在客户端环境中使用,只能在 Firebase 应用程序开发人员拥有或管理的特权服务器环境中使用。我有点不知道如何解决这个问题。是否可以像我使用 firebaseApi 那样将 firebase 云功能连接到客户端?我已经清理了代码,只保留了相关部分

firebaseApi.js

        import firebase from 'firebase/app';
        import 'firebase/firestore';
        import 'firebase/auth';
        import 'firebase/database';
        import 'firebase/storage';

        const config = {config};

        firebase.initializeApp(config);

        class Firebase {
          register = ({ fullname, email, phone }) => {
            const user = Firebase.auth.currentUser.uid;
            const firestoreRef = Firebase.firestore.collection('Users').doc(user);

            const settings = {
              fullname,
              email,
              phone,
            };

            firestoreRef
              .set(settings);
          };

          static init() {
            Firebase.auth = firebase.auth();
            Firebase.firestore = firebase.firestore();
            Firebase.database …
Run Code Online (Sandbox Code Playgroud)

node.js firebase reactjs firebase-authentication google-cloud-functions

0
推荐指数
1
解决办法
1687
查看次数