【发布时间】:2019-07-06 00:58:31
【问题描述】:
很抱歉,这似乎是一个非常基本的问题,云功能的概念对我来说是非常新的,我仍然处于学习过程中。
但是,在尝试执行此云功能时,我收到以下错误
TypeError: Cannot read property 'data' of undefined
完整日志可见here
作为参考,我没有做这个功能,我只是想让它工作,我用this视频。
实际的云功能:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const firestore = admin.firestore();
const settings = { timestampInSnapshots: true };
firestore.settings(settings);
const stripe = require('stripe')(functions.config().stripe.token);
exports.addStripeSource =
functions.firestore.document('cards/{userid}/tokens/{tokenid}')
.onCreate(async (tokenSnap, context) => {
var customer;
const data = tokenSnap.after.data();
if (data === null) {
return null
}
const token = data.tokenId;
const snapchat = await
firestore.collection('cards').doc(context.params.userId).get();
const customerId = snapshot.data().custId;
const customerEmail = snpashot.data().email;
if (customerId === 'new') {
customer = await stripe.customers.create({
email: customerEmail,
source: token
});
firestore.collection('cards').doc(context.params.userId).update({
custId: customer.id
});
}
else {
customer = await stripe.customers.retrieve(customerId)
}
const customerSource = customer.sources.data[0];
return firestore.collection('cards').doc(context.params.userId).collection('sources').doc(customerSource.card.fingerprint).set(customersource, { merge: true });})
我用于编写支付服务的 dart 代码:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class PaymentService {
addCard(token) {
FirebaseAuth.instance.currentUser().then((user) {
print("Found User");
Firestore.instance
.collection('cards')
.document(user.uid)
.collection('tokens')
.add({'tokenId': token}).then((val) {
print('saved');
});
});
}
}
最后,当我按下按钮时会执行什么:
StripeSource.addSource().then((String token) {
print("Stripe!");
PaymentService().addCard(token);
});
正如您所看到的,代码显然被触发了,但我猜 data var 存在某种错误,JavaScript 对我来说是全新的,所以我确定它是某种非常愚蠢的语法问题。
【问题讨论】:
-
这行代码中".onCreate(async(tokenSnap, conetxt) => {"上下文拼写错误
-
道歉@randomSoul,我不知何故错过了这一点,我设法在询问我的实际问题和在 StackOverflow 上发布之间拼写了我的代码。多么尴尬!对于那个很抱歉!更新了日志和我的代码。
-
数据似乎是一个属性,但在您的代码
const data = tokenSnap.after.data();中它被指定为一个函数。尝试将其更改为tokenSnap.after.data。通过console.log(tokenSnap)检查你在“tokenSnap”变量中得到了什么。 -
@JacobPyke 你有这个运气吗...我正在学习相同的教程!
-
我使用相同的代码,但它现在不起作用。我复制了你的代码,但同样的错误?
标签: javascript dart flutter google-cloud-firestore google-cloud-functions