簡體   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