我有一个带有排行榜的iOS /快速游戏,我希望分数在每个星期一的12:00 am都重置为0。
我都设置了Cloud Functions,并且index.ts中有代码,这些代码将在每个星期一的凌晨12:00运行,但是我不确定如何在TypeScript中编写代码以将所有userHighScores更新为0。 / p>
这是我到目前为止在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’);
});
保存以上代码并在Terminal中运行“ 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 am更新数据库中所有用户的所有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);
});
});