javascript - 如何修复每个 then() 应该返回一个值或抛出
问题描述
我正在尝试为 firebase 通知构建服务器,并且我观看了一个 youtube 视频,该视频展示了如何编写该 javascript 代码,我复制并粘贴了代码,同时调整了一些路径以匹配我的 firebase 数据库,但我有不知道出了什么问题,我有点不知道如何用 javascript 编写。所以这是代码:
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) => {
const receiver_user_id = context.params.receiver_user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to :' , receiver_user_id);
if (!data.after.val()) {
console.log('A notification has been deleted :' , notification_id);
}
const sender_user_id=admin.database().ref('/Notifications/${receiver_user_id}/${notification_id}').once('value');
return sender_user_id.then(fromUserResult => {
const from_sender_user_id=fromUserResult.val().from;
console.log('you have a notification from :',sender_user_id);
const userQuery=admin.database.ref('/Users/${receiver_user_id}/messaging_token`').once('value');
return userQuery.then(userResult => {
const senderUserName=userResult.val();
return null;
});
const DeviceToken = admin.database().ref(`/users/${receiver_user_id}/messaging_token`).once('value');
return DeviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
from_sender_user_id:from_sender_user_id,
title: "New Chat Request",
body: `${senderUserName} offered you a lesson, Please Check.`,
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload)
.then(response => {
console.log('This was a notification feature.');
});
});
});
});
这些是我得到的错误和警告:
26:44 warning Unexpected template string expression no-template-curly-in-string
31:38 warning Unexpected template string expression no-template-curly-in-string
32:10 warning Avoid nesting promises promise/no-nesting
38:3 error Unreachable code no-unreachable
40:10 warning Avoid nesting promises promise/no-nesting
任何帮助都会非常感激:)
解决方案
前两个警告是由这一行引起的:
const sender_user_id=admin.database().ref('/Notifications/${receiver_user_id}/${notification_id}').once('value');
而这一行:
const userQuery=admin.database.ref('/Users/${receiver_user_id}/messaging_token`').once('value');
在这两种情况下,您${...}
在构建路径时都在字符串中使用代码中的值。这称为模板文字的扩展。但是您的字符串用单引号括起来'
,这意味着它不会被解释为模板字符串。
解决方案是在代码中使用反引号。所以:
const sender_user_id=admin.database().ref(`/Notifications/${receiver_user_id}/${notification_id}`).once('value');
和:
const userQuery=admin.database.ref(`/Users/${receiver_user_id}/messaging_token`').once('value`);
请注意,您不应在声明 Cloud Function ( exports.sendNotification = functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')
) 的行中使用反引号,因为该字符串实际上应该传递给 firebase-functions 模块。
有关此错误的更多信息,请参阅错误消息的 ESLint 文档。我建议您找到收到的每个警告/错误的解释,并尝试先自己修复它,因为它可以防止被否决。
推荐阅读
- types - Cudnn 是否支持 INT32 数据类型?
- java - Eclipse JavaFX 不显示 GUI
- python - 尝试读取源 docx 文件并将其转录为目标 .docx 文件。不工作的是页眉/页脚部分
- r - 如何使用 RMarkdown 使用胶水包进行折叠?
- xamarin.forms - 一个 emptyView 用于加载数据,另一个用于没有可用数据时
- django - 尽管尝试了解决方案,但试图理解为什么 Selenium 在“/app/.apt/usr/bin/google-chrome”错误中看不到二进制 - 使用 Heroku
- javascript - 如果条件与三元运算符不返回真或假
- javascript - Angular 中的 Observables 和订阅
- javascript - 如何使用 Chai 确保 Promise 抛出错误?
- pyomo - 为什么 Pyomo 中 Set 的输出是无序的?