JavaScript的新手,非常感谢您的耐心等待。我想编写一个Google Cloud Function,该函数将在输入数据库时发送一条消息。它必须从数据库中的多个位置获取数据。以下是我到目前为止的内容:
exports.NotifyNewApplication = functions.database
.ref(`/Applications/{applicationId}/appWith`)
.onCreate((snapshot, context) => {
var applicationId = context.params.applicationId;
console.log('status', applicationId);
var recieverId = snapshot.val();
var database = admin.database().ref(`/Applications/${applicationId}`);
return database.once('value').then(snapshot=>{
var nameId= snapshot.child("dogId").val();
console.log('status', snapshot.child('nameId').val());
console.log('status', snapshot.child('appWith').val());
var userDatabase = admin.database().ref(`/users/TopUsers/${recieverId}/token`)
return userDatabase.once('value').then(snapshot=>{
var token = snapshot.val();
console.log('status', snapshot.val());
var nameDatabase = admin.database().ref(`/names/${nameId}/name`)
return nameDatabase.once('value').then(snapshot=>{
var Name= snapshot.val();
const message = {
notification: {
title: 'Application Recieved',
body: 'Somebody has put an application in for '+Name,
icon: "default"
},
};
return admin.messaging().sendToDevice(token,message).then((response) => {
return console.log('Successfully sent message:', response);
}).catch((error) => {
return console.log('Error sending message:', error);
});
});
});
});
});
但是,我的工作原理是
38:16 warning Avoid nesting promises promise/no-nesting
42:16 warning Avoid nesting promises promise/no-nesting
51:16 warning Avoid nesting promises promise/no-nesting
51:16 warning Avoid nesting promises promise/no-nesting
讨论嵌套承诺,并查看每个区域的返回行。我的问题是,我似乎无法理解如何取消套利并获得相同的工作结果。
任何人的指导,我将不胜感激。
答案 0 :(得分:0)
与其在回调中链接promise.then()
调用,不如返回promise
并将.then()
链接在链接的最外层。
由于有些变量需要在下一个then
回调中使用(例如nameId
),因此可以使用Promise.all
将数组传递给链中的下一个回调,并且在其中添加此类变量:
exports.NotifyNewApplication = functions.database
.ref(`/Applications/{applicationId}/appWith`)
.onCreate((snapshot, context) => {
var applicationId = context.params.applicationId;
console.log('status', applicationId);
var recieverId = snapshot.val();
var database = admin.database().ref(`/Applications/${applicationId}`);
return database.once('value')
.then(snapshot=>{
var nameId= snapshot.child("dogId").val();
console.log('status', snapshot.child('nameId').val());
console.log('status', snapshot.child('appWith').val());
var userDatabase = admin.database().ref(`/users/TopUsers/${recieverId}/token`)
return Promise.all([nameId, userDatabase.once('value')]);
}).then(([nameId, snapshot])=>{
var token = snapshot.val();
console.log('status', snapshot.val());
var nameDatabase = admin.database().ref(`/names/${nameId}/name`)
return Promise.all([token, nameDatabase.once('value')]);
}).then(([token, snapshot])=>{
var Name= snapshot.val();
const message = {
notification: {
title: 'Application Recieved',
body: 'Somebody has put an application in for '+Name,
icon: "default"
},
};
return admin.messaging().sendToDevice(token,message);
}).then((response) => {
return console.log('Successfully sent message:', response);
}).catch((error) => {
return console.log('Error sending message:', error);
});