Firebase onWrite其他实例event.params未定义

时间:2018-02-15 10:29:11

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



exports.cloneUsers = functions.database.instance('ams4-contract').ref('/users/{user_id}').onWrite(event => {
//exports.cloneUsers = functions.database.ref('/users/{user_id}').onWrite(event => {
    let user_data = event.data.val();
    let user_id = event.params.user_id;

    console.log('event usr ' + event.timestamp);
    console.log('Indexing usr ' + user_id + ' ' + user_data);

    let elasticSearchFields = ['name', 'nick', 'martial art'];
    let elasticSearchConfig = functions.config().elasticsearch;
    let elasticSearchUrl = elasticSearchConfig.url + 'users/user/' + user_id;
    let elasticSearchMethod = user_data ? 'POST' : 'DELETE';

    let elasticSearchRequest = {
        method: elasticSearchMethod,
        uri: elasticSearchUrl,
        auth: {
            username: elasticSearchConfig.username,
            password: elasticSearchConfig.password
        },
        body: _.pick(user_data, elasticSearchFields),
        json: true
    };

    return request(elasticSearchRequest).then(response => {
        console.log('Elasticsearch response', response);
    })

});




当我使用functions.database.ref时, 它可以获得event.params.user_id 它可以更新elasticsearch complete。

但请使用functions.database.instance('ams4-contract') event.params.user_id未定义!

此日志 functions.database.ref的日志ID 36608977830579 functions.database.instance('ams4-contract')的日志ID 36604231411908 谢谢。

Output

1 个答案:

答案 0 :(得分:0)

这似乎是一个错误。您现在可以通过在使用instance()时删除ref字符串中的前导斜杠来解决它:

exports.cloneUsers = functions.database.instance('ams4-contract')
    .ref('users/{user_id}')

我在内部使用Firebase提交了一个错误。您可以自己file a bug report随意。