【发布时间】:2019-10-10 04:51:53
【问题描述】:
我不知道我做错了什么,所以需要额外的眼睛来弄清楚我做错了什么。 我正在尝试来自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);
}
但只要我推送数据,它就会在几秒钟内被删除。
做错了什么?
【问题讨论】:
-
我是否正确理解您的代码分为两个文件,并带有导出/导入功能?
-
是的!它分为两个文件。只是为了让我的 index.js 清晰。
-
您能否在您的问题中添加两个文件的确切内容,而不仅仅是一些 sn-ps。 Function 中的代码可以正常工作,因此可能是由于模块化。
-
我已经更新了问题,如果我可以添加更多信息,请告诉我。
标签: javascript firebase firebase-realtime-database google-cloud-functions