node.js firebase部署错误 [英] node.js firebase deploy error

查看:87
本文介绍了node.js firebase部署错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**当我尝试部署时出现此错误* 我是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)代码

this is my node.js(index.js) code

'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');

  });

  });

 });

 });

推荐答案

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

This code refactoring should do the trick:

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版以上的代码


Updated: code for version 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;

这篇关于node.js firebase部署错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆