У меня есть игра для iOS / swift с таблицей лидеров, и я хотел бы, чтобы все баллы сбрасывались до 0 каждый понедельник в 12:00 утра.код в моем index.ts, который будет запускаться каждый понедельник в 12:00, но я не уверен, как написать код в TypeScript, чтобы обновить все userHighScores до 0.
Вот что у меня есть в индексе.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» в Терминале я вижу эту ошибку:
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 неверна.Какой будет правильный синтаксис?
Заранее спасибо за любую помощь.
Обновление
Вот рабочий кодкоторый обновляет все userHighScores для всех пользователей в базе данных один раз в неделю в понедельник в 12:00:
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);
});
});