I am trying to test using firebase-functions-test. I'm wondering if I've done something wrong in the setup, because I've noticed that my function stops after trying to access the database.
const config = {
databaseURL: '******,
projectId: '*****'
};
const test = require('firebase-functions-test')(config, './devKey.json');
const myFunctions = require('../index.js');
const wrapped = test.wrap(myFunctions.search.fetchProfiles);
let before = test.database.makeDataSnapshot('', 'cloud-functions/fetchProfiles/
-L8eDyqVjH2WcsnfFDj3/request');
let after = test.database.makeDataSnapshot({
version: '1.0.8',
userIds: ['-L4dcm7BSctgsqAzpeOP']
}, 'cloud-functions/fetchProfiles/-L8eDyqVjH2WcsnfFDj3/request' );
let change = test.makeChange(before, after);
let context = {
params: {userId: '-L8eDyqVjH2WcsnfFDj3'}
};
wrapped(change, context);
Then, when this line of code is ran within the function:
data.after.ref.parent.child('state').once('value')
It is not successful. The function has no problems when deployed. In particular, are my data snapshots created properly?
This was fixed in version 0.1.1 of firebase-functions-test.
I have no idea why, but it's 3 weeks later now and I tried it, and now it works. I don't recall changing anything.
Related
My error code after calling the below (which is shortened for brevity) within a cloud functions is:
Error: 3 INVALID_ARGUMENT: Location 'europe-west1' is not a valid location. Use ListLocations to list valid locations.
If I change to location to "us-central" for example, the error code changes to:
Error: 3 INVALID_ARGUMENT: Location must equal europe-west1 because the App Engine app that is associated with this project is located in europe-west1
I have had a look on stackoverflow for similar but came up short. I left a comment on this question to see if the op had any luck:
Google Cloud Tasks: Location 'europe-west1' is not a valid location
Where am I going wrong?
Thanks!
JT
OS: Google Cloud Functions
Node.js version: 12
npm version: 6.14.10
#google-cloud/tasks version: 2.3.0
Steps to reproduce
const functions = require('firebase-functions');
const {CloudTasksClient} = require("#google-cloud/tasks");
const admin = require('firebase-admin');
const client = new CloudTasksClient({ fallback: true });
// Omit the actual triggered function
async function createHttpTasks(session) {
const project = "XXXXX"; // These match my project and queue id/name
const queue = "XXXXXX";
const location = "europe-west1";
// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);
// Do stuff
const requestCheck = {parent, taskCheckIn};
await client.createTask(requestCheck);
}
I had two things wrong:
const client = new CloudTasksClient({ fallback: true });
should have been:
const client = new CloudTasksClient();
and
const requestCheck = {parent, taskCheckIn};
should have been
const requestCheck = {parent, task: taskCheckIn};
thanks to the gcloud team for responding to my issue:
https://github.com/googleapis/nodejs-tasks/issues/509
If anyone else is getting this error, make sure there are no spaces in the location variable. For example, there is a space at the end of the variable:
location="europe-west-3 "
I use the "testcafe-browser.provider-browserstack" plugin for TestCafé. I try to use this in my app, but I always get a message that the login is wrong. The data is right (just "test" for this post)
const createTestCafe = require('testcafe');
const BrowserStack = require("testcafe-browser-provider-browserstack");
let testcafe = null;
process.env.BROWSERSTACK_USERNAME="test";
process.env.BROWSERSTACK_PASSWORD="test";
createTestCafe('localhost', 1337, 1338)
.then(tc => {
testcafe = tc;
const runner = testcafe.createRunner();
return runner
.src(['tests/fixture1.js', 'tests/ingo1.js'])
.browsers('browserstack:Chrome')
//.browsers(['chrome'])
.screenshots('allure/screenshots/', true)
.reporter('allure')
.run();
})
.then(failedCount => {
console.log('Tests failed: ' + failedCount);
testcafe.close();
});
Please use the BROWSERSTACK_ACCESS_KEY environment variable instead of BROWSERSTACK_PASSWORD. Refer to the following article to get details
I am writing a node.js skill using ask-sdk and using alexa-skill-local to test the endpoint. I need to persist data to DynamoDb in one of the handler. But I keep getting "missing region error". Please find my code below:
'use strict';
// use 'ask-sdk' if standard SDK module is installed
const Alexa = require('ask-sdk');
const { launchRequestHandler, HelpIntentHandler, CancelAndStopIntentHandler, SessionEndedRequestHandler } = require('./commonHandlers');
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
return handlerInput.responseBuilder
.speak('Sorry, I can\'t understand the command. Please say again.')
.reprompt('Sorry, I can\'t understand the command. Please say again.')
.getResponse();
},
};
////////////////////////////////
// Code for the handlers here //
////////////////////////////////
exports.handler = Alexa.SkillBuilders
.standard()
.addRequestHandlers(
launchRequestHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
ErrorHandler
)
.withTableName('devtable')
.withDynamoDbClient()
.lambda();
And in one of the handler I am trying to get persisted attributes like below:
handlerInput.attributesManager.getPersistentAttributes().then((data) => {
console.log('--- the attributes are ----', data)
})
But I keep getting the following error:
(node:12528) UnhandledPromiseRejectionWarning: AskSdk.DynamoDbPersistenceAdapter Error: Could not read item (amzn1.ask.account.AHJECJ7DTOPSTT25R36BZKKET4TKTCGZ7HJWEJEBWTX6YYTLG5SJVLZH5QH257NFKHXLIG7KREDKWO4D4N36IT6GUHT3PNJ4QPOUE4FHT2OYNXHO6Z77FUGHH3EVAH3I2KG6OAFLV2HSO3VMDQTKNX4OVWBWUGJ7NP3F6JHRLWKF2F6BTWND7GSF7OVQM25YBH5H723VO123ABC) from table (EucerinSkinCareDev): Missing region in config
at Object.createAskSdkError (E:\projects\nodejs-alexa-sdk-v2-eucerin-skincare-dev\node_modules\ask-sdk-dynamodb-persistence-adapter\dist\utils\AskSdkUtils.js:22:17)
at DynamoDbPersistenceAdapter.<anonymous> (E:\projects\nodejs-alexa-sdk-v2-eucerin-skincare-dev\node_modules\ask-sdk-dynamodb-persistence-adapter\dist\attributes\persistence\DynamoDbPersistenceAdapter.js:121:45)
Can we read and write attributes from DynamoDb using alexa-skill-local ? Do we need some different setup to achieve this ?
Thanks
I know that this is a really old topic, but I had the same problem few days ago, and I'm gonna explain how I did it work.
You have to download DynamoDB Locally and follow the instructions from here
Once that you have configure your local DynamoDB and check that it is working. You have to pass it through your code, to DynamoDbPersistenceAdapter constructor.
Your code should look similar to:
var awsSdk = require('aws-sdk');
var myDynamoDB = new awsSdk.DynamoDB({
endpoint: 'http://localhost:8000', // If you change the default url, change it here
accessKeyId: <your-access-key-id>,
secretAccessKey: <your-secret-access-key>,
region: <your-region>,
apiVersion: 'latest'
});
const {DynamoDbPersistenceAdapter} = require('ask-sdk-dynamodb-persistence-adapter');
return new DynamoDbPersistenceAdapter({
tableName: tableName || 'my-table-name',
createTable: true,
dynamoDBClient: myDynamoDB
});
Where <your-acces-key-id>, <your-secrete-access-key> and <your-region> are defined at aws config and credentials files.
The next step is launch your server with alexa-skill-local command as always.
Hope this will be helpfull! =)
Presumably you have an AWS config profile that your skill is using when running locally.
You need to edit the .config file and set the default region (ie us-east-1) there. The region should match the region where your table exists.
Alternatively, if you want to be able to run completely isolated, you may need to write come conditional logic and swap the dynamo client with one targeting an instance of DynamoDB Local running on your machine.
Follow the guide of using Cloud Functions with Firebase.
Setup environment
setup project in Firebase
Created function
command prompt writes that function deployed, but firebase is empty.
I am new with deploying functions so I am sure that it is stupid question and I think I did something wrong in setting up but I checked three times different guides and it looks everything done right. So please if you know what the problem it is can be?
I used this guide and there I done everything till initializing the project
https://firebase.google.com/docs/functions/get-started
After that in index.js I wrote a function
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(function.config().firebase);
export.sendNotification = functions.database
.ref('/notifications/{user_id}/{notification_id}')
.onWrite(event => {
conts user_id = event.params.user_id;
const notification = event.params.notification_id;
if(!event.data.val()){
return console.log('A notification has been deleted ', notification_id);
}
const payload = {
notification: {
title: "Friend Request",
body: "Received new Friend Request",
icon: "default"
}
};
return admin.messaging().sendToDevice(/*Token*/, payload).then(response =>{
console.log('');
});
});
And with the command
firebase deploy
I tried to deploy function
But in firebase cattegory "Function" it is still empty
Error in CMD
There is syntax error, Please change below line in your code
admin.initializeApp(function.config().firebase);
to
admin.initializeApp(functions.config().firebase);
I'm quite late here but it might help somebody else. You're right, it is typo. The correct command is:
exports
not
export
I'm trying to use lambda to restore a table in dynamoDB,
but keep getting this error message.
TypeError: dynamodb.restoreTableFromBackup is not a function
Could anyone tell me how to fix it?Thanks~
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#restoreTableFromBackup-property
'use strict';
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = (event, context, callback) => {
const params = {
BackupArn: 'arn:aws:dynamodb:us-east-1/xxxxx',
TargetTableName: 'xxx',
};
dynamodb.restoreTableFromBackup(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
};
I ran the code you provided in my own lambda and got the same result.
It seems the AWS SDK that is provided natively in Lambda isn't the latest version, with the restoreTableFromBackup function.
If I do the same test while using the latest available in npm, it seems to work fine.
As a work around, I suggest uploading your lambda as a zip file, and including the the node_modules/aws-sdk along with your code.
This will ensure it uses the latest aws-sdk which includes the restoreTableFromBackup function.
To assist further, I've written a walk through of how to use On-Demand backups / restore and how to schedule these backups.
https://www.abhayachauhan.com/2017/12/dynamodb-scheduling-on-demand-backups
HTH
'npm install aws-sdk' in Cloud9 terminal for the Lambda