Android 使用Firestore的Firebase奖励推荐
我正在尝试部署一个简单的Firebase云函数Android 使用Firestore的Firebase奖励推荐,android,node.js,firebase,google-cloud-firestore,Android,Node.js,Firebase,Google Cloud Firestore,我正在尝试部署一个简单的Firebase云函数 exports.createUser = functions.firestore .document('users/{userId}') .onCreate(event => { let documentRef = admin.firestore.doc(`users/${event.data.referredBy}/cats/accountInformation`); console.lo
exports.createUser = functions.firestore
.document('users/{userId}')
.onCreate(event => {
let documentRef = admin.firestore.doc(`users/${event.data.referredBy}/cats/accountInformation`);
console.log("Error giving hints:", documentRef);
return documentRef.data.ref.set({
credits: credits + 5
.catch(error => {
console.log("Error giving hints:", error);
}),
}, {
merge: true
});
});
我想使用Firestore在推荐人注册时用应用内积分奖励推荐人
这是我的node.js
函数
exports.createUser = functions.firestore
.document('users/{userId}')
.onCreate(event => {
let documentRef = admin.firestore.doc(`users/${event.data.referredBy}/cats/accountInformation`);
console.log("Error giving hints:", documentRef);
return documentRef.data.ref.set({
credits: credits + 5
.catch(error => {
console.log("Error giving hints:", error);
}),
}, {
merge: true
});
});
我对node.js非常不熟悉。我环顾四周,尝试了不同的语法,但没有一个有效。任何帮助都将不胜感激。我找不到太多关于这方面的文档。以下是一个示例,说明如何在不发出另一个请求的情况下拖动书面信息,仅使用onCreate firestore事件:
exports.createUser = functions.firestore.document('users/{userId}').onCreate(event => {
//Doc ref
let documentRef = admin.firestore.doc(`users/${event.data.referredBy}/cats/accountInformation`);
//Get the value you want from that document
documentRef.get().then((doc) => {
var docData = doc.data();
var currentCredits = docData.credits;
var newCredits = currentCredits + 5;
//Then you return the promise, this time pointing the new ref
return documentRef.set({credits:newCredits});
});
});
我讨厌上这个网站。如果一个人发了一个问题,其他人也可能面临同样的问题。将OP指向正确的方向,而不是无缘无故地贬低投票职位的愚蠢的不渐进行为。如果你想要一些建设性的反馈,我的建议是多解释一下你已经尝试过的东西。这些代码有效吗?我很确定,
credits:credits+5
不起作用,因为你需要先查询当前用户的原始信用,然后操作数字,然后写回。我只是尝试了不同的语法,没有保存代码。我对node.js不太熟悉,因此无法重新创建我编写的内容。我看不出我过去的尝试与我现在的问题有什么关系。这就是上面的函数在语法上是不正确的。@KarloA.López它不起作用。我认为这可能是问题的一部分,但我不知道如何获取credits字段中的值并将其应用于变量。我觉得它不应该在return函数中,但我不确定。好吧,问题是credits
值不是来自事件
数据返回的Firestore文档,而是来自documentRef
变量引用的不同文档。这是因为我想奖励推荐人,而不是刚刚注册的用户。我将如何引用该文件?这似乎是我还没有弄明白的主要问题。谢谢你到目前为止的帮助。很抱歉,没有阅读问题的细节,请查看我的更新答案。啊!可以我无法真正理解get
函数。我将尝试部署您的答案,并在测试出来后几分钟内检查它是否仍然不起作用。出于某种原因,我的控制台上的每个日志都是空白的。由于某些原因,该函数未被触发。我将尝试联系Firebase支持。如果你知道问题是什么,请告诉我。否则,直到那时。您是否正在部署该功能?还是仅仅为它服务?