Javascript aws lambda nodejs在另一个异步函数中访问异步函数的值
我有一个获取firebase数据值的函数,即联系人:Javascript aws lambda nodejs在另一个异步函数中访问异步函数的值,javascript,asynchronous,firebase,aws-lambda,alexa-skills-kit,Javascript,Asynchronous,Firebase,Aws Lambda,Alexa Skills Kit,我有一个获取firebase数据值的函数,即联系人: function firebaseData (callback) { if(firebase.apps.length == 0) { firebase.initializeApp({ serviceAccount: "./Sample1-bc6d1ce099d8.json", databaseURL:"https://sample1-74bc2.firebaseio.com/" }); }
function firebaseData (callback) {
if(firebase.apps.length == 0) {
firebase.initializeApp({
serviceAccount: "./Sample1-bc6d1ce099d8.json",
databaseURL:"https://sample1-74bc2.firebaseio.com/"
});
}
var ref = firebase.database().ref();
var usersRef = ref.child('users');
usersRef.once('value').then(function (snap) {
snap.forEach(function(childSnapshot) {
var childKey = childSnapshot.val();
var contact = childKey.Contact;
callback(contact)
});
});
}
我想要的是在作为联系地点的其他功能中使用此联系值数据:
function firebaseData (callback) {
if(firebase.apps.length == 0) {
firebase.initializeApp({
serviceAccount: "./Sample1-bc6d1ce099d8.json",
databaseURL:"https://sample1-74bc2.firebaseio.com/"
});
}
var ref = firebase.database().ref();
var usersRef = ref.child('users');
usersRef.once('value').then(function (snap) {
snap.forEach(function(childSnapshot) {
var childKey = childSnapshot.val();
var contact = childKey.Contact;
callback(contact)
});
});
}
function getContactFromSession(intent, session, callback) {
const repromptText = null;
const sessionAttributes = {};
let shouldEndSession = false;
let speechOutput = "amy's contact is"+CONTACT;
// Setting repromptText to null signifies that we do not want to reprompt the user.
// If the user does not respond or says something that is not understood, the session
// will end.
callback(
sessionAttributes,
buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession)
);
}
这怎么办,有人能给我提个建议吗?也许是这样的?在
firebaseData()
的回调中调用getContactFromSession()
的回调
function firebaseData (callback) {
if(firebase.apps.length == 0) {
firebase.initializeApp({
serviceAccount: "./Sample1-bc6d1ce099d8.json",
databaseURL:"https://sample1-74bc2.firebaseio.com/"
});
}
var ref = firebase.database().ref();
var usersRef = ref.child('users');
usersRef.once('value').then(function (snap) {
snap.forEach(function(childSnapshot) {
var childKey = childSnapshot.val();
var contact = childKey.Contact;
callback(contact)
});
});
}
使用承诺可能有助于使其更具可读性。您可以调用firebase的
usersRef.once('value')
函数中的getContactFromSession(intent,session,callback)
函数吗?@AmreshVenugopal Ya但最终需要在getContactFromSession(intent,session,callback)中获取联系人值谢谢,我以前也试过这个,但是调用它会产生错误:“响应无效”还有其他方法吗?
function firebaseData (callback) {
if(firebase.apps.length == 0) {
firebase.initializeApp({
serviceAccount: "./Sample1-bc6d1ce099d8.json",
databaseURL:"https://sample1-74bc2.firebaseio.com/"
});
}
var ref = firebase.database().ref();
var usersRef = ref.child('users');
usersRef.once('value').then(function (snap) {
snap.forEach(function(childSnapshot) {
var childKey = childSnapshot.val();
var contact = childKey.Contact;
callback(contact)
});
});
}