【发布时间】:2019-10-05 19:40:36
【问题描述】:
我有一个带有排行榜的 iOS/swift 游戏,我希望在每周一上午 12:00 将分数全部重置为 0。
我已全部设置好 Cloud Functions,并且我的 index.ts 中有代码,这些代码将在每周一上午 12:00 运行,但我不确定如何在 TypeScript 中编写代码以将所有 userHighScores 更新为 0。
这是我目前在 index.ts 中的内容:
import * as functions from 'firebase-functions';
functions.pubsub.schedule(‘0 0 * * 1’).onRun((context) => {
// This code should set userHighScore to 0 for all users, but isn't working
.ref('/users/{user.user.uid}/').set({userHighScore: 0});
console.log(‘This code will run every Monday at 12:00 AM UTC’);
});
保存上述代码并在终端中运行“firebase deploy”后,我看到的错误如下:
Found 23 errors.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely
additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/derencewalk/.npm/_logs/2019-05-19T00_39_38_037Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code2
当我 Firebase 仅部署 console.log 代码时没有任何错误,所以我很确定这只是 .ref 代码行格式错误。正确的语法是什么?
提前感谢您的帮助。
更新
这是每周一上午 12:00 更新数据库中所有用户的所有 userHighScores 的工作代码:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
export const updateHighScores = functions.pubsub.schedule('0 0 * * 1').onRun((context) => {
//console.log(‘This code will run every Monday at 12:00 AM UTC’);
const db = admin.database();
return db
.ref('users')
.once('value')
.then(snapshot => {
const updates:any = {};
snapshot.forEach((childSnapshot:any) => {
const childKey = childSnapshot.key;
updates['users/' + childKey + '/userHighScore'] = 0;
updates['users/' + childKey + '/earnedExtraTime'] = 0;
});
return db.ref().update(updates);
});
});
【问题讨论】:
标签: typescript firebase google-cloud-functions