Ошибка FireBase Ошибка разбора: неожиданный возврат токена - PullRequest
0 голосов
/ 17 мая 2018

Я пытаюсь сделать Push-уведомления.поэтому, когда я развертываю свой код в firebase, я получаю следующую ошибку при попытке загрузить функции firebase

Это ошибка, отображаемая в моем терминале:

   22:14  warning  Avoid nesting promises                      promise/no-nesting
   22:69  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.

   npm ERR! A complete log of this run can be found in:
   npm ERR!     /Users/mac/.npm/_logs/2018-05-17T17_45_54_673Z-debug.log

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

Код JavaScript:

'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {
        const user_id = context.params.user_id;
        const notification = context.params.notification;
        console.log('The User Id is : ', user_id);
        if(!event.data.vall()){
            return console.log('A notification has been deleted from the database : ',notification_id);
        }
        const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
            return deviceToken.then(result =>{
                        const token_id = result.val();
                        const payload = {
                            notification: {
                                title : "Friend Request",
                                body : "You have received a new Friend Request",
                                icon : "default"
                            }
                        };
             return admin.messaging().sendToDevice(token_id, payload).then(response =>{
                console.log("This was the notification Feature");
                    });
        });
});

Как это исправить?

Ответы [ 2 ]

0 голосов
/ 11 июля 2019

Ответ прост, каждый из которых затем должен вернуть значение
. Вы должны добавить оператор возврата, например

ответ возврата

скопировать и вставить этот кодбудет работать

'использовать строгие'
const functions = require ('firebase-functions');
const admin = require ('firebase-admin');
admin.initializeApp (functions.config (). firebase);
exports.sendNotification = functions.database.ref ('/ notifications / {user_id}
/ {tification_id}'). onWrite ((change, context) =>{
const user_id = context.params.user_id;
const messages = context.params.notification;
console.log ('Идентификатор пользователя:', user_id);
if (! Event.data.vall ()) {
return console.log ('Из базы данных удалено уведомление:', response_id);
} ​​
const deviceToken = admin.database (). ref (/Users/${user_id}<br>/device_token). Once ('значение');
return deviceToken.then (result => {
const token_id = result.val ();
const payload = {
нетtification: {
title: "Запрос друга",
body: "Вы получили новый запрос друга",
icon: "default"
}
};
return admin.messaging (). sendToDevice (token_id, payload)
.then (response => {
console.log («Это была функция уведомления»);
ответ возврата
});
});
});

0 голосов
/ 17 мая 2018

У вас есть два возврата:

return return admin.messaging().sendToDevice(token_id, payload).then(response =>{ console.log("This was the notification Feature"); });

Заменить на:

return admin.messaging().sendToDevice(token_id, payload).then(response =>{
            console.log("This was the notification Feature");
                });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...