��权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - ReferenceError: context is not defined - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReferenceError: context is not defined - Stack Overflow

programmeradmin3浏览0评论

I am trying to create a sendNotification function by using node.js and firebase function but unfortunately I am currently stuck. I never got a notification from the sender and when I look to Firebase function logs, I saw an error says

ReferenceError: context is not defined
at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:9:32)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
at /var/tmp/worker/worker.js:700:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

this is my code index.js code

`const functions = require('firebase-functions');
 const admin = require('firebase-admin');
 admin.initializeApp(functions.config().firebase);
 exports.sendNotification = functions.database.ref('notifications/{user_id}').onWrite(event => {

const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

console.log('We have notification from: ', user_id);

if (!event.val()) {
    return console.log('A Notification has been deleted from the database: ', notification_id);
}

const fromUser = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

return fromUser.then(result => {

    const token_id = result.val();

    const payload = {
        notification: {
            title: "Friend Request",
            body: "You've received a Friend Request",
            icon: "default"
        }
    };

    return admin.messaging().sendToDevice(token_id , payload).then(response => {

        console.log('This was the notification feature');

    });

});

});

I don't know where is the error here can someone tell me where is the error and how can I resolve it?

I am trying to create a sendNotification function by using node.js and firebase function but unfortunately I am currently stuck. I never got a notification from the sender and when I look to Firebase function logs, I saw an error says

ReferenceError: context is not defined
at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:9:32)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
at /var/tmp/worker/worker.js:700:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

this is my code index.js code

`const functions = require('firebase-functions');
 const admin = require('firebase-admin');
 admin.initializeApp(functions.config().firebase);
 exports.sendNotification = functions.database.ref('notifications/{user_id}').onWrite(event => {

const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

console.log('We have notification from: ', user_id);

if (!event.val()) {
    return console.log('A Notification has been deleted from the database: ', notification_id);
}

const fromUser = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

return fromUser.then(result => {

    const token_id = result.val();

    const payload = {
        notification: {
            title: "Friend Request",
            body: "You've received a Friend Request",
            icon: "default"
        }
    };

    return admin.messaging().sendToDevice(token_id , payload).then(response => {

        console.log('This was the notification feature');

    });

});

});

I don't know where is the error here can someone tell me where is the error and how can I resolve it?

Share Improve this question edited Apr 17, 2018 at 10:08 Peter Haddad 80.9k25 gold badges145 silver badges146 bronze badges asked Apr 17, 2018 at 5:21 user8724610user8724610 3
  • 1 It's exactly as the error says - you never define context before you use it...? – CertainPerformance Commented Apr 17, 2018 at 5:22
  • Okay, so how can I define the context here? – user8724610 Commented Apr 17, 2018 at 5:24
  • I don't know, it's your app - you seem to have something in mind when you access its .params.user_id. Figure out what that object is, and assign it to context before you use it – CertainPerformance Commented Apr 17, 2018 at 5:26
Add a ment  | 

2 Answers 2

Reset to default 2

Change it to this:

exports.sendNotification = functions.database.ref('notifications/{user_id}').onWrite((change,context) => {

const user_id = context.params.user_id;

});

onWrite has two parameters change and context. You use context to access the wildcards example user_id.

more info here:

https://firebase.google./docs/functions/beta-v1-diff

I managed to solved my own problem by the help of @PeterHaddad. I read the link that he post and I change some of my code. Here it is

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

exports.sendNotification = functions.database.ref('notifications/{user_id}').onWrite((change, context) => {

const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

console.log('We have notification from: ', user_id);

if (!change.after.val()) {
    return console.log('A Notification has been deleted from the database: ', notification_id);
}

const fromUser = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

return fromUser.then(result => {

    const token_id = result.val();

    const payload = {
        notification: {
            title: "Friend Request",
            body: "You've received a Friend Request",
            icon: "default"
        }
    };

    return admin.messaging().sendToDevice(token_id , payload).then(response => {

        console.log('This was the notification feature');

    });

});

});
发布评论

评论列表(0)

  1. 暂无评论