I have a database in Firebase to which I'm trying to write some data from within my Alexa Skill. The Node.js code for that skill sits inside an AWS Lambda function and when that code is run I want to push some data to Firebase.
I've tested the code that connects to and pushes to Firebase outside of Lambda and it works exactly as expected. Below is that code:
var firebase = require('firebase');
firebase.initializeApp({
databaseURL: 'https://myapp.firebaseio.com',
serviceAccount: './myapp.json',
});
var cart = firebase.database().ref('/cart');
console.log(cart);
cart.push( {
item: 'apples', quantity: '1', amount: '0'
}, function(error) {
if (error) {
console.log("Data could not be saved." + error);
} else {
console.log("Data saved successfully.");
}
});
This same code doesn't push anything to the database instance when executed from within the Lambda function. I read online that Lambda timeout limit could be a reason for this, so I increased the timeout limit to a minute, and it still doesn't run as expected. I've also tried using the Firebase REST API instead of their Node.js SDK and that didn't work either. What is the correct way to push data to Firebase from within AWS Lambda?
With Firebase consisting of proprietary services, APIs, and an SDK, a migration to AWS requires application refactoring – introducing a new architecture using AWS services, and rewriting parts of the codebase to use them accordingly.
PDFRSS. You can use Lambda functions to build services that give new skills to Alexa, the Voice assistant on Amazon Echo. The Alexa Skills Kit provides the APIs, tools, and documentation to create these new skills, powered by your own services running as Lambda functions.
I think I know why this happens, I had a similar issue and this is what I've done.
If you want to write some date into your database you need to make sure that you don't call this.emit(*****) until you are done. As soon as you return the response to the user the Thread gets closed and your information doesn't get saved.
The easiest way to solve this problem is to return the response to the user once you get confirmation that the information has been saved.
In case of Firebase something like this:
function writeUserData(userId) {
// Get a key for a new Post.
var userKey = db.ref('users/').push().key;
var reference = db.ref('users/' + userKey).set({
user: userId
});
reference.then(() => {
alexa.emit(':ask', "Works");
},
(err) => {
alexa.emit(':ask', "Does not work");
});
}
I couldn't get anything saved until I started doing it like this.
Hope it helps.
I've run into this too and the only way I've figured out how to get it to work is to delay the lambda callback function in the handler. Try this and let me know if it works.
var firebase = require('firebase');
firebase.initializeApp({
databaseURL: 'https://myapp.firebaseio.com',
serviceAccount: './myapp.json',
});
exports.handler = (event, context, callback) => {
var cart = firebase.database().ref('/cart');
console.log(cart);
cart.push( {
item: 'apples', quantity: '1', amount: '0'
setTimeout(()=>{
callback(null, 'success');
},2000);
}, function(error) {
if (error) {
console.log("Data could not be saved." + error);
setTimeout(()=>{
callback(error);
},2000);
} else {
console.log("Data saved successfully.");
setTimeout(()=>{
callback(null, 'success');
},2000);
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With