Firebase功能出现问题

时间:2019-01-19 23:17:33

标签: javascript firebase firebase-realtime-database google-cloud-functions

我正在尝试使用此功能删除实时数据库中的旧条目。我无法删除旧记录,有什么想法吗?该功能似乎运行良好。下面的代码。

module.exports = (api) => {
api.cache(false);

return {
    presets: [
    '@emotion/babel-preset-css-prop',
    [
        '@babel/preset-env',
        {
        modules: false,
        loose: true,
        targets: {
            browsers: ['last 2 versions'],
        },
        },
    ],
    '@babel/preset-react',
    ],
    plugins: [
    'react-hot-loader/babel',
    [
        'transform-imports',
        {
        lodash: {
            transform: 'lodash/${member}',
            preventFullImport: true,
        },
        },
    ],
    ['import', { libraryName: 'antd', libraryDirectory: 'lib', style: true }],
    ['@babel/plugin-proposal-class-properties'],
    '@babel/plugin-proposal-async-generator-functions',
    '@babel/plugin-proposal-object-rest-spread',
    '@babel/plugin-transform-modules-commonjs',
    ],
};
};

数据库的结构如下...

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const CUT_OFF_TIME = 2 * 60 * 60 * 1000; // 2 Hours in milliseconds.

 exports.deleteOldItems = functions.database.ref('/posts/{pushId}')
 .onWrite((change, context) => {
   var ref = change.after.ref.parent; // reference to the items
   var now = Date.now();
   var cutoff = now - 600;
   var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
   return oldItemsQuery.once('value', function(snapshot) {
     var updates = {};
     snapshot.forEach(function(child) {
       updates[child.key] = null
     });

     return ref.update(updates);
   });
 });

1 个答案:

答案 0 :(得分:1)

您不能同时使用promise和Once()的回调。您应该选择一个。通常最好选择使用诺言。

return oldItemsQuery.once('value').then(snapshot => {
   var updates = {};
   snapshot.forEach(child => {
     updates[child.key] = null;
   });
   return ref.update(updates);
});