【问题标题】:node.js firebase deploy errornode.js firebase 部署错误
【发布时间】:2018-10-26 21:44:55
【问题描述】:

*当我尝试部署时出现此错误 * 我是 java 脚本的新手 .. 正在创建与聊天选项共享的帖子我想收到来自 firebase 的通知,所以我正在使用此代码,但部署时出现错误请帮助我,我正在寻找解决方案,从 6 小时开始但我没找到

62:12  warning  Avoid nesting promises             promise/no-nesting
88:14  warning  Avoid nesting promises               promise/no-nesting
88:69  error    Each then() should return a value or throw promise/always-return

✖ 3 problems (1 error, 2 warnings)

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 likelyadditional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/alpha/.npm/_logs/2018-05-16T18_06_07_691Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit   code1

 Having trouble? Try firebase deploy --help

这是我的 node.js(index.js) 代码

'use strict'

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

 /*
   * 'OnWrite' works as 'addValueEventListener' for android. It will fire   the function
   * everytime there is some item added, removed or changed from the provided 'database.ref'
  * 'sendNotification' is the name of the function, which can be changed according to
   * your requirement
 */



    exports.sendNotification =   functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite(event => {


 /*
  * You can store values as variables from the 'database.ref'
  * Just like here, I've done for 'user_id' and 'notification'
  */

  const user_id = event.params.user_id;
  const notification_id = event.params.notification_id;

  console.log('We have a notification from : ', user_id);

  /*
   * Stops proceeding to the rest of the function if the entry is deleted from database.
   * If you want to work with what should happen when an entry is deleted, you can replace the
   * line from "return console.log.... "
   */

  if(!event.data.val()){

 return console.log('A Notification has been deleted from the  database : ', notification_id);

 }

 /*
  * 'fromUser' query retreives the ID of the user who sent the  notification
  */

 const fromUser =    admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');

 return fromUser.then(fromUserResult => {

const from_user_id = fromUserResult.val().from;

console.log('You have new notification from  : ', from_user_id);

/*
 * The we run two queries at a time using Firebase 'Promise'.
 * One to get the name of the user who sent the notification
 * another one to get the devicetoken to the device we want to send notification to
 */

const userQuery = admin.database().ref(`Users/${from_user_id}/name`).once('value');
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

return Promise.all([userQuery, deviceToken]).then(result => {

  const userName = result[0].val();
  const token_id = result[1].val();

  /*
   * We are creating a 'payload' to create a notification to be sent.
   */

  const payload = {
    notification: {
      title : "New Friend Request",
      body: `${userName} has sent you request`,
      icon: "default",
      click_action : "in.tvac.akshaye.lapitchat_TARGET_NOTIFICATION"
    },
    data : {
      from_user_id : from_user_id
    }
  };

  /*
   * Then using admin.messaging() we are sending the payload notification to the token_id of
   * the device we retreived.
   */

  return admin.messaging().sendToDevice(token_id, payload).then(response => {

    console.log('This was the notification Feature');

  });

  });

 });

 });

【问题讨论】:

  • 你的代码里是什么? .onWrit e(event => {
  • 抱歉,剪切粘贴时出错了..
  • 是的,请提供任何解决方案
  • 我也试过 Peter Haddad

标签: android node.js firebase google-cloud-functions


【解决方案1】:

这个代码重构应该可以解决问题:

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

/*
  * 'OnWrite' works as 'addValueEventListener' for android. It will fire   the function
  * everytime there is some item added, removed or changed from the provided 'database.ref'
 * 'sendNotification' is the name of the function, which can be changed according to
  * your requirement
*/


exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite(event => {


    /*
     * You can store values as variables from the 'database.ref'
     * Just like here, I've done for 'user_id' and 'notification'
     */

    const user_id = event.params.user_id;
    const notification_id = event.params.notification_id;

    console.log('We have a notification from : ', user_id);

    /*
     * Stops proceeding to the rest of the function if the entry is deleted from database.
     * If you want to work with what should happen when an entry is deleted, you can replace the
     * line from "return console.log.... "
     */

    if (!event.data.val()) {

        console.log('A Notification has been deleted from the  database : ', notification_id);

        return false;
    }

    /*
     * 'fromUser' query retreives the ID of the user who sent the  notification
     */

    const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');

    return fromUser
        .then(fromUserResult => {

            const from_user_id = fromUserResult.val().from;
            console.log('You have new notification from  : ', from_user_id);

            /*
             * The we run two queries at a time using Firebase 'Promise'.
             * One to get the name of the user who sent the notification
             * another one to get the devicetoken to the device we want to send notification to
             */

            const userQuery = admin.database().ref(`Users/${from_user_id}/name`).once('value');
            const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

            return Promise.all([userQuery, deviceToken]);

        })
        .then(result => {

            const userName = result[0].val();
            const token_id = result[1].val();

            const payload = {
                notification: {
                    title: "New Friend Request",
                    body: `${userName} has sent you request`,
                    icon: "default",
                    click_action: "in.tvac.akshaye.lapitchat_TARGET_NOTIFICATION"
                },
                data: {
                    from_user_id: from_user_id
                }
            };

            return admin.messaging().sendToDevice(token_id, payload);

        })
        .then(response => {

            console.log('This was the notification Feature');
            return true;

        }).catch(error => {

          console.log(error);
          //any other error treatment
        });
});

更新:1.0+ 版代码

exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {


  const user_id = context.params.user_id;
  const notification_id = context.params.notification_id;

【讨论】:

  • 请注意,我已对其进行了修改以添加错误捕获,这是强制性的。观看 Firebase 团队的以下视频:youtube.com/watch?v=7IkUgCLr5oA&t=16syoutube.com/watch?v=652XeeKNHSk&t=8s
  • 还有一件事:请注意,最近发布了 Firebase Cloud Functions 的新版本,语法发生了重要变化。见firebase.google.com/docs/functions/beta-v1-diff。也许您可以在成功使用您正在使用的语法(版本
  • 它部署成功,但在日志中我无法获取 user_id
  • Cannot read property 'user_id' of undefined 它在日志中显示这个
  • 您使用的是哪个版本的 Cloud Functions?您可以在“依赖项”节点下的 package.json 文件中看到(文件位于函数目录中)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 2016-01-05
  • 2017-01-21
  • 1970-01-01
  • 2021-04-10
  • 2017-12-12
相关资源
最近更新 更多