Javascript 解决云函数中的“TypeError:无法读取未定义的”属性“data”
很抱歉,如果这看起来是一个非常基本的问题,那么云函数的概念对我来说是非常新的,我仍然处于高度的学习过程中 然而,在尝试执行这个云函数时,我得到了以下错误 TypeError:无法读取未定义的属性“data” 可以看到完整的日志 作为参考,我没有做这个函数,我只是想让它工作,我用的是视频 实际的云功能:Javascript 解决云函数中的“TypeError:无法读取未定义的”属性“data”,javascript,dart,flutter,google-cloud-firestore,google-cloud-functions,Javascript,Dart,Flutter,Google Cloud Firestore,Google Cloud Functions,很抱歉,如果这看起来是一个非常基本的问题,那么云函数的概念对我来说是非常新的,我仍然处于高度的学习过程中 然而,在尝试执行这个云函数时,我得到了以下错误 TypeError:无法读取未定义的属性“data” 可以看到完整的日志 作为参考,我没有做这个函数,我只是想让它工作,我用的是视频 实际的云功能: const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.i
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);
});
正如您所看到的,代码显然被触发了,但我猜数据变量中存在某种错误,JavaScript对我来说是全新的,因此我确信这是某种非常愚蠢的语法问题。正如您提供的日志输出所解释的:您需要为firestore.document函数定义一个引用:
functions.firestore.document('cards/{userid}/tokens/{tokenid}')
将其修改为:
functions.firestore.documentReference(){
}
从附加的日志映像中,错误为未定义上下文 在上面的函数中,您已将参数作为conetxt传递,稍后将在函数上下文中使用,因此它将给出未定义的错误
将参数名conetxt更改为context。在这行代码中。onCreateAync tokenSnap,conetxt=>{上下文拼写错误抱歉@randomSoul,我不知怎么错过了,在问我的实际问题和在StackOverflow上发布之间,我设法拼错了我的代码。太尴尬了!抱歉!更新了日志和我的代码。看起来数据是一个属性,但在您的代码中,const data=tokenSnap.after.data;它被指定为function。尝试将其更改为tokenSnap.after.data。通过执行console.logtokenSnap来检查“tokenSnap”变量中的内容。@JacobPyke您在这方面运气好吗…我正在学习相同的教程!我使用相同的代码,但它现在不起作用。我复制了您的代码,但出现了相同的错误?抱歉,我更新了代码,因为存在拼写错误rega根据上下文参数,这个答案在我的更新中仍然有效吗?是的,我已经更改了它
functions.firestore.document('cards/{userid}/tokens/{tokenid}')
.onCreate(async (tokenSnap, conetxt) => {