javascript - Cloud 函数不响应 Cloud Firestore 中的 onCreate
问题描述
我有一个 index.js 文件,其中包含以下内容:
const functions = require('firebase-functions');
const trackVote = require('./trackVote')
const admin = require('firebase-admin');
admin.initializeApp();
exports.trackVote = trackVote.handler;
然后我引用以下函数:
trackvote.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.handler = functions.firestore.document('/Polls/{pollId}/responses/{userId}').onCreate((data, context) => {
const answerSelected = data.data().answer;
const answerRef = admin.firestore().doc(`Polls/${context.params.pollId}/answers/${answerSelected}`);
const voteCountRef = admin.firestore().doc(`Polls/${context.params.pollId}`);
return admin.firestore().runTransaction(t => {
return t.get(answerRef)
.then(doc => {
if (doc.data()) {
t.update(answerRef, { vote_count: doc.data().vote_count + 1 });
}
})
}).then(result => {
return admin.firestore().runTransaction(t => {
return t.get(voteCountRef)
.then(doc => {
if (doc.data()) {
t.update(voteCountRef, {vote_count:doc.data().vote_count+1});
}
});
});
});
});
不幸的是,没有调用此方法,但是我知道我正在写入 Cloud Firestore 中的特定位置。我的 Firebase 仪表板中也没有任何日志。
以下是写入我的 Cloud Firestore 的代码:
mPollQuestionRadioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
disableVoteButtons(group);
Toast.makeText(getActivity().getApplicationContext(), R.string.vote_submitted_label, Toast.LENGTH_LONG).show();
voteOnPoll((Integer) group.findViewById(checkedId).getTag());
//TODO: add animation between between vote click and chart display
mPollQuestionRadioGroup.setVisibility(View.INVISIBLE);
mPollResults.setVisibility(View.VISIBLE);
}
});
方法:
private void voteOnPoll(int checkedRadioButtonIndex) {
int selectedAnswerIndex = checkedRadioButtonIndex + 1;
HashMap<String, Object> firebaseAnswer = new HashMap<>();
firebaseAnswer.put("answer", selectedAnswerIndex);
mStoreBaseRef.collection("Polls").document(pollID).collection("responses").document(mUserID).set(firebaseAnswer);
}
当我在仪表板中检查 Cloud Firestore 时,我可以确认此位置已成功写入。
解决方案
推荐阅读
- javascript - 检查字符串中的单词是否与数组中的单词匹配,如果匹配则将其从字符串中删除
- javascript - 否则,角度选择类
- python - Python解析xml找不到元素
- ios - 当应用程序在后台时,我可以访问 AppDelegate 内的应用程序变量吗?
- antlr4 - ANTLR4 空格问题,将数字与字母分开
- python - 具有混合 dtypes 的 pandas DataFrame 的类型提示
- asp.net - Visual Studio 属性 Web 选项卡开始操作特定页面:如何将此值发布到服务器?
- amadeus - Amadeus Flight Offers Search API 自定义搜索
- javascript - 交互式 SVG 地图 Javascript
- excel - Excel VBA Worksheet_Change 未计算问题