简体   繁体   中英

node.js referenceError variable is not defined

I am trying to implement device groups in my node.js file to trigger notifications.

I want to get the token_id and create a notification_key for the device groups for a single user.

but i am getting the error as.

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)

and my index.js file is.

'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;




      });


 });

});

 });

I am new to node.js as well.

Secondly, is my index.js file correct? or I am doing something wrong?.

Your options object is defined after its first use:

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

Define

var options = { ...

before it is used in

request(options ...

Hope that helps :)

Without initializing variable named option , your are passing it as a parameter for the request. Thats why you are getting following error ReferenceError: options is not defined . Here I commented the unused part. checkout this snippet now

'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;




      });


 });

});

 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM