我无法弄清楚我在做什么错,因此需要额外的眼睛来找出我在做什么错。 我正在尝试从Delete firebase data older than 2 hours获得问题的答案,该问题的答案是使用https://github.com/firebase/functions-samples/tree/master/delete-old-child-nodes和https://github.com/firebase/functions-samples/blob/master/delete-old-child-nodes/functions/index.js在一段时间后删除节点。
/root
/items
LoHgJSFt8hHi2o_hP: {
timestamp: 1497911193083,
...some_other_data
},
LoHgJSsGGHi2o_fD: {
timestamp: 1597911193083
...some_other_data
}
我的index.js
看起来像
//index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { createUser } = require('./auth/onCreate');
const { deleteOldData } = require('./auth/onDeleteOldData');
const serviceAccount = require('./ownerstown-admin.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://ownexxxxxxxx-c86cf.firebaseio.com'
});
exports.createUser = functions.firestore.document('users/{userId}').onCreate(createUser);
exports.deleteOldData = functions.database.ref('/referralIds/ids/{pushId}').onWrite(deleteOldData);
然后是onDeleteOldData.js
// auth/onDeleteOldData.js
const CUT_OFF_TIME = 2 * 60 * 60 * 1000;
async function deleteOldData(change) {
console.log('starting nooowwwwwww');
const ref = change.after.ref.parent;
console.log('ref', ref);
const now = Date.now();
const cutoff = now - CUT_OFF_TIME;
const oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
const snapshot = await oldItemsQuery.once('value');
console.log('snapshot', snapshot);
const updates = {};
snapshot.forEach(child => {
updates[child.key] = null;
});
return ref.update(updates);
}
module.exports = {
deleteOldData
};
并按一下按钮将数据推送
function createLoginId() {
const rootRef = Firebase.database.ref();
const allDetails = rootRef.child('referralIds/ids');
const timeStamp = Date.now();
const someId = uuidv4();
const { origin } = window.location;
const data = {
timeStamp,
someId,
createrId: id
};
const newRef = allDetails.push();
newRef.set(data);
}
但是一旦我推送数据,它就会在几秒钟内被删除。
怎么了?
答案 0 :(得分:1)
问题来自于这样一个事实:当您写入数据库时,您会进行操作
const timeStamp = Date.now();
//...
const data = {
timeStamp,
someId,
createrId: id
};
这意味着您保存了字段timeStamp
,但是您的Cloud Function根据timestamp
选择要删除的节点(全部为小写)。
实时数据库中的节点名称区分大小写。
顺便说一句,请注意,您可以使用时间戳记
const timestamp = firebase.database.ServerValue.TIMESTAMP;
“用于自动填充Firebase服务器确定的当前时间戳的占位符”,请参见https://firebase.google.com/docs/reference/js/firebase.database.ServerValue