繁体   English   中英

未定义node.js referenceError变量

[英]node.js referenceError variable is not defined

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

您的options对象是在首次使用后定义的:

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

限定

var options = { ...

在使用之前

request(options ...

希望有帮助:)

如果不初始化名为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;




      });


 });

});

 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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