【问题标题】:node.js referenceError variable is not definednode.js referenceError 变量未定义
【发布时间】:2018-05-23 14:15:04
【问题描述】:

我正在尝试在我的 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 文件是否正确?还是我做错了什么?

【问题讨论】:

    标签: android node.js firebase


    【解决方案1】:

    如果不初始化名为option 的变量,您会将其作为请求的参数传递。这就是为什么您会收到以下错误ReferenceError: options is not defined。在这里,我评论了未使用的部分。立即查看此 sn-p

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

    【讨论】:

    • 是的。没看到。它现在完美无缺。还有一件事。我的代码有风险吗?..我是 node.js 的新手。谢谢
    • 我建议您在将其移至生产环境时将安全密​​钥/参数保留为环境变量。例如:授权服务帐户密钥
    【解决方案2】:

    您的选项对象是在第一次使用后定义的:

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

    定义

    var options = { ...
    

    在使用之前

    request(options ...
    

    希望有帮助:)

    【讨论】:

      猜你喜欢
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      相关资源
      最近更新 更多