【问题标题】:Firebase Cloud Functions / FirestoreFirebase 云功能 / Firestore
【发布时间】:2018-05-09 22:52:09
【问题描述】:

每当创建新客户时,我都会尝试运行 firebase 云功能来为 Angular 5 项目进行原子(批量)更新。

我不断收到此错误,无法找出问题所在:

控制台错误

错误:无法在 Function.encodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:658:11 处将类型 ([object Undefined]) 编码为 Firestore 值)

云函数 (index.js)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const stripe = require('stripe')(functions.config().stripe.testkey);

// 1. Create Stripe User on sign up

exports.createStripeCustomer = functions.auth.user().onCreate(event => {
    // user auth data
    const user = event.data;

    // register Stripe user
    return stripe.customers.create({
        email: user.email
    })
    .then(customer => {
        const db = admin.firestore();
        const batch = db.batch();
        const currentTime = this.timestamp;
        const customerDoc = db.collection('customers').doc(customer.id);
        const userDoc = db.collection('users').doc(user.uid);

        batch.set(customerDoc, {userId: user.uid, timestamp: currentTime });
        batch.set(userDoc, {customerId: customer.id, timestamp: currentTime });

        return batch.commit();

    });
});

【问题讨论】:

    标签: javascript node.js firebase google-cloud-functions google-cloud-firestore


    【解决方案1】:

    我刚刚发现了错误。我正在定义currentTimethis.timestamp,但我从未定义过timestamp

    【讨论】:

    • 我还想说,也许你的问题是在 Promise 中调用它。如果您稍后遇到未定义的问题,只需在 then() 调用之前定义一个 let self = this 并使用 self.timestamp
    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2018-06-30
    • 2020-06-09
    • 2018-06-25
    相关资源
    最近更新 更多