【问题标题】:Error on deploy of firebase push notification function部署 Firebase 推送通知功能时出错
【发布时间】:2018-03-11 04:53:33
【问题描述】:

我正在尝试为我的应用使用 Firebase 推送通知服务。为此,我在我的应用程序中设置了 firebase 推送通知依赖项,并且还安装了 node.js 和 firebase 工具。当我在目录上使用 firebase deploy 来部署功能时,我收到此错误

40:14  warning  Avoid nesting promises                      promise/no-
nesting
41:25  error    Each then() should return a value or throw  promise/always-
return

✖ 2 problems (1 error, 1 warning)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional 
logging output above.

这是 index.js 文件

'use strict'


const functions = require('firebase-functions');
const admin = require ('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
       .onWrite(event => 
	   {
		   const receiver_id = event.params.receiver_id;
		   
		   const notification_id = event.params.notification_id;
		   
		   console.log('We have a notification to send to : ', receiver_id);
		   
		   if(!event.data.val()) 
		   {
			   return console.log('A notification has been deleted from the database : ', notification_id);
		   }
		   
		   
		   const deviceToken = admin.database().ref(`/Users/${receiver_id}/device_token`).once('value');
		   
		   
		   return deviceToken.then(response => 
		   {
			   const token_id = result.val();
			   
			   const payload = 
			   {
				   notification: 
				   {
					   title: "Friend Request",
					   body: "You have a new friend request",
					   icon: "default"
				   }
			   };
			   
			   return admin.messaging().sendToDevice(token_id, payload)
			               .then(response => 
						   {
							   console.log('This was the notification feature');
						   });
	        });  	  
       });
	   

我该如何解决这个问题?我的朋友使用相同的代码,它成功上传,没有错误

【问题讨论】:

    标签: firebase firebase-realtime-database push-notification


    【解决方案1】:

    来自 ESLint 的错误信息是这样的:

    41:25  error    Each then() should return a value or throw  promise/always-return
    

    它抱怨的代码是这样的:

                           .then(response => 
                           {
                               console.log('This was the notification feature');
                           });
    

    消息是说每个 then() 方法回调都应该返回一个值或抛出一个异常。如果您在日志后的回调中简单地 return null 可能会有所帮助。

    您还会收到有关嵌套承诺的警告。您的代码在另一个 then() 中不使用 then() 更具可读性。您可以简单地将它们在同一级别上相互链接以获得相同的效果。

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 2020-09-09
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 2018-06-01
      • 1970-01-01
      相关资源
      最近更新 更多