代码之家  ›  专栏  ›  技术社区  ›  rituparna

未定义node.js referenceerror变量

  •  0
  • rituparna  · 技术社区  · 8 年前

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

    我要获取令牌\u id并为单个用户创建设备组的通知\u密钥。

    但我得到的错误是。

    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 回复  |  直到 8 年前
        1
  •  0
  •   Ashok JayaPrakash    8 年前

    不初始化名为的变量 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;
    
    
    
    
          });
    
    
     });
    
    });
    
     });
    
        2
  •  0
  •   sjdm    8 年前

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

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

    定义

    var options = { ...
    

    在它被用于

    request(options ...
    

    希望有帮助:)

    推荐文章