【发布时间】:2018-08-06 03:06:21
【问题描述】:
我是新的 android 初学者并尝试部署 firebase 功能但显示一些错误如何解决这个问题请帮助我。
Firebase 数据库结构
用户表
- 用户
- user_id
- device_token : user_device_token
- 名称:用户名
- user_id
通知表
- 通知
- to_user_id
- notification_id
- 来自:from_user_id
- notification_id
- to_user_id
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",
}
};
/*
* 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');
});
});
});
});
**cmd 显示错误**
C:\Users\TahirAliAwan\Desktop\Function>firebase 部署 === 部署到“videochat-96f75”... 我部署功能 运行命令:npm --prefix %RESOURCE_DIR% run lint 函数@ lint C:\Users\TahirAliAwan\Desktop\Function\functions 埃斯林特。 C:\Users\TahirAliAwan\Desktop\Function\functions\index.js 61:11 警告避免嵌套承诺承诺/不嵌套 84:14 警告避免嵌套承诺承诺/不嵌套 84:69 错误每个 then() 都应该返回一个值或抛出 promise/always-return ✖ 3 个问题(1 个错误,2 个警告) npm 错误! Windows_NT 10.0.10586 npm 错误! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "--prefix" "C:\Users\TahirAliAwan\Desktop\函数\函数" "运行" "lint" npm 错误!节点 v6.11.5 npm 错误! npm v3.10.10 npm 错误!代码生命周期 npm 错误!函数@ lint:
eslint .npm 错误!退出状态 1 npm 错误! npm 错误!函数@ lint 脚本“eslint .”失败。 npm 错误!确保您安装了最新版本的 node.js 和 npm。 npm 错误!如果这样做,这很可能是函数包的问题, npm 错误!不是 npm 本身。 npm 错误!告诉作者这在您的系统上失败: npm 错误!埃斯林特。 npm 错误!您可以通过以下方式获取有关如何为此项目打开问题的信息: npm 错误! npm 错误功能 npm 错误!或者,如果这不可用,您可以通过以下方式获取他们的信息: npm 错误! npm 所有者 ls 函数 npm 错误!上面可能有额外的日志输出。 npm 错误!请在任何支持请求中包含以下文件: npm 错误! C:\Users\TahirAliAwan\Desktop\Function\npm-debug.log 错误:函数预部署错误:命令以非零退出代码终止1
【问题讨论】:
-
您是否两次检索
userid?同时删除 .then函数你不会得到错误 -
在 Stack Overflow 上显示错误时,请将错误的整个文本复制到问题中,而不是显示图像。这样更容易阅读和搜索。
-
您的错误输出显示来自 TSLint 的两个警告和一个关于始终从 then() 返回值的错误。在所有这些消息之间,它建议您重构代码中的承诺。
-
@Tahiraliawan 你删除了
.then(response => { console.log('This was the notification Feature');吗? -
@Peter Haded 我删除了这一行,但还剩下一个错误。 ` 61:11 警告避免嵌套承诺承诺/不嵌套`
标签: node.js firebase google-cloud-functions eslint