避免嵌套函数,Google 云函数
Avoid Nested Function, Google Cloud Function
JavaScript 的新手,感谢您的耐心等待...我正在寻找一个 Google 云函数,它将在数据库条目上发送消息。它必须从数据库中的多个位置获取数据。以下是我目前所拥有的:
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
讨论嵌套承诺并查看每个区域的 return 行。我的问题是我似乎无法理解如何取消嵌套 return 并获得相同的工作结果。
如果有人提供任何指导,我将不胜感激。
不是在回调中链接 promise.then()
调用,只需 return 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);
});
JavaScript 的新手,感谢您的耐心等待...我正在寻找一个 Google 云函数,它将在数据库条目上发送消息。它必须从数据库中的多个位置获取数据。以下是我目前所拥有的:
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
讨论嵌套承诺并查看每个区域的 return 行。我的问题是我似乎无法理解如何取消嵌套 return 并获得相同的工作结果。
如果有人提供任何指导,我将不胜感激。
不是在回调中链接 promise.then()
调用,只需 return 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);
});