node.js没有定义referenceError变量

时间:2018-05-23 06:09:30

标签: android node.js firebase

我正在尝试在node.js文件中实现设备组以触发通知。

我想获取token_id并为单个用户创建设备组的notification_key。

但是我收到了错误。

ReferenceError: options is not defined
at Object.<anonymous> (D:\SweExpress\functions\index.js:13:9)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Module.require (internal/modules/cjs/loader.js:626:17)
at require (internal/modules/cjs/helpers.js:20:18)
at C:\Users\user\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:21:11
at Object.<anonymous> (C:\Users\user\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:61:3)

和我的index.js文件是。

'use-strict'

 const functions = require('firebase-functions');
 const admin = require('firebase-admin');
 var request = require('request');
 admin.initializeApp();
 var headers = {
 'Authorization': 'key =  my service account key here',
 'project_id': 'server_id',
 'Content-Type': 'application/json'
 }

 request(options, function (error, response, body) {
 console.log(body)
 })

 exports.sendNotification = functions.firestore.document("Users/  {user_id}/Notifications/{notification_id}").onWrite((change,context) => {

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

return admin.firestore().collection("Users").doc(user_id).collection("Notifications").doc(notification_id).get().then(queryResult => {

 const from_user_id = queryResult.data().from;
 const from_message = queryResult.data().message;


 const from_data = admin.firestore().collection("Users").doc(from_user_id).get();
 const to_data = admin.firestore().collection("Users").doc(user_id).get();

 return Promise.all([from_data, to_data]).then(result => {



     const from_name = result[0].data().name;
     const to_name = result[1].data().name;
     const token_id = result[1].data().token_id;

     var options = {
      url: 'https://android.googleapis.com/gcm/notification',
      method: 'POST',
      headers: headers,
      json: {'operation': 'create',
      'notification_key_name': 'you@example.com',
      'registration_ids': [token_id]}
      }

      request(options, function (error, response, body) {
      console.log(body)
      })

      var notificationKey = body;

     const payload = {
        notification: {
            title: "Notification From: " + from_name,
            body : from_message,
            icon : "notification.png",
            sound: "orion.mp3"
        }
     };
      return admin.messaging().sendToDeviceGroup(notificationKey,payload).then(result => {
      var c = console.log("Notification sent ");
        return c;




      });


 });

});

 });

我也是node.js的新手。

其次,我的index.js文件是否正确?或者我做错了什么?

2 个答案:

答案 0 :(得分:0)

您的选项对象在首次使用后定义:

$result['tests'] = Test::select('test.test_id',
    DB::raw('convert_tz(test.updated_at,+00:00,-05:30) as updated_at'),
    'test.created_at')
    ->orderBy('test.test_id', 'ASC')
    ->get()->toArray();

定义

request(options, function (error, response, body) {
  console.log(body)
})

中使用之前

var options = { ...

希望有所帮助:)

答案 1 :(得分:0)

如果没有初始化名为option的变量,则将其作为请求的参数传递。这就是为什么你得到以下错误ReferenceError: options is not defined。在这里,我评论了未使用的部分。现在结帐这个片段

'use-strict'

 const functions = require('firebase-functions');
 const admin = require('firebase-admin');
 var request = require('request');
 admin.initializeApp();
 var headers = {
 'Authorization': 'key =  my service account key here',
 'project_id': 'server_id',
 'Content-Type': 'application/json'
 }

 /* Code which causes error
 request(options, function (error, response, body) {
 console.log(body)
 })*/

 exports.sendNotification = functions.firestore.document("Users/  {user_id}/Notifications/{notification_id}").onWrite((change,context) => {

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

return admin.firestore().collection("Users").doc(user_id).collection("Notifications").doc(notification_id).get().then(queryResult => {

 const from_user_id = queryResult.data().from;
 const from_message = queryResult.data().message;


 const from_data = admin.firestore().collection("Users").doc(from_user_id).get();
 const to_data = admin.firestore().collection("Users").doc(user_id).get();

 return Promise.all([from_data, to_data]).then(result => {



     const from_name = result[0].data().name;
     const to_name = result[1].data().name;
     const token_id = result[1].data().token_id;

     var options = {
      url: 'https://android.googleapis.com/gcm/notification',
      method: 'POST',
      headers: headers,
      json: {'operation': 'create',
      'notification_key_name': 'you@example.com',
      'registration_ids': [token_id]}
      }

      request(options, function (error, response, body) {
      console.log(body)
      })

      var notificationKey = body;

     const payload = {
        notification: {
            title: "Notification From: " + from_name,
            body : from_message,
            icon : "notification.png",
            sound: "orion.mp3"
        }
     };
      return admin.messaging().sendToDeviceGroup(notificationKey,payload).then(result => {
      var c = console.log("Notification sent ");
        return c;




      });


 });

});

 });