我是上述所有技术的新手。因此,如果这个问题不存在,我提前致歉。
因此,我正在尝试编写与firebase提供的示例stripe-firebase实时数据库-云功能集成示例相同的firestore。它位于:https://github.com/firebase/functions-samples/blob/master/stripe/functions/index.js
我成功转换了exports.createStripeCustomer函数。但是我在使用addPaymentSource时遇到麻烦。
这是我到目前为止所拥有的:
exports.addPaymentSource = functions.firestore.document('Users/{userId}/paymentSources/{paymentID}').onWrite((change, context) => {
let newPaymentSource = change.after.data();
let token = newPaymentSource.token;
return functions.firestore.document(`Users/${context.params.userId}`).get('customer_data')
.then((snapshot) => {
return snapshot.val();
}).then((customer) => {
return stripe.customers.createSource(customer, {newPaymentSource});
}).then((response) => {
return change.after.ref.parent.set(response);
}, (error) => {
return change.after.ref.parent.child('error').set(userFacingMessage(error));
}).then(() => {
return reportError(error, {user: context.params.userId});
});
});
部署成功,但是功能失败,并显示以下错误。
TypeError:functions.firestore.document(...)。get不是一个函数 在exports.addPaymentSource.functions.firestore.document.onWrite(/user_code/index.js:73:77) 在对象。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) 在下(本机) 在/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在__awaiter(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在cloudFunction(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) 在/var/tmp/worker/worker.js:728:24 在process._tickDomainCallback(internal / process / next_tick.js:135:7)
显然,“获取”似乎不是一个函数。我从以下参考文档中获得了此功能。 https://firebase.google.com/docs/reference/functions/functions.firestore.DocumentSnapshot
我似乎缺少一些东西。我已经坚持了几天。因此,任何帮助将不胜感激。提前致谢。
答案 0 :(得分:0)
使用firebase-admin
const admin = require('firebase-admin');
exports.addPaymentSource = functions.firestore.document('Users/{userId}/paymentSources/{paymentID}').onWrite((change, context) => {
let newPaymentSource = change.after.data();
let token = newPaymentSource.token;
return admin.firestore().doc(`Users/${context.params.userId}`).get('customer_data')
// what you have to do
});
});
答案 1 :(得分:0)
功能包用于触发功能。您可以使用以下方法:
(更多信息:https://firebase.google.com/docs/firestore/extend-with-functions)
要使用get()方法,您需要使用firebase-admin软件包-然后可以从admin.firestore()访问firestore
(firebase-admin:https://www.npmjs.com/package/firebase-admin)