cloud function testing failing "Invalid request, unable to process." - javascript

I have written a simple cloud function which I also deployed already to firebase and the code works. It is the following function:
exports.testfunction = functions.https.onCall(async (data) => {
const mydata = JSON.parse(data)
const mydata1 = JSON.parse({const1: "AAA", const2: "BBB", const3: "CCC"})
return{mydata1}
});
Now that I want to test this function localy, Im running the following command in my terminal: firebase emulators:start and I get no errors so far. Anyways as soon as I try to call the function with http://localhost:5001/MYPROJECTNAME/us-central1/testfunction in my browser, Im receiving the following error message which I can not find any workaround for:
{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}
The thing is, the function itself is working great if I deploy it and call it from inside my app, but Im failing to run it from my firebase emulator inside the console.
What am I doing wrong here?

As explained in the documentation for Callable Cloud Functions:
It's important to keep in mind that HTTPS callable functions are
similar but not identical to HTTP functions. To use HTTPS callable
functions you must use the client SDK for your platform together with
the functions.https backend API (or implement the protocol).
So, if you want to directly call the function via it's URL, your HTTP Request needs to follow the protocol specification for https.onCall.

Related

AWS SAM: How to locally invoke all Node.js functions instead one by one?

I am developing a REST API with AWS Lambda, API Gateway, RDS (MySQL). I am using Node.js. I am also using AWS SAM tool.
In my API I have lambda functions that accept URL parameters and that do not accept them.
I noticed that I can deploy these to AWS without an issue, and then execute from POSTMAN.
However when I try to locally run them, I am running into problems.
When I execute sam local invoke, it says Error: You must provide a function logical ID when there are more than one functions in your template. So I had to execute them one by one, like sam local invoke FunctionName
When I try to invoke a function that accepts URL parameters, it says TypeError: Cannot destructure property 'id' of 'event.queryStringParameters' as it is undefined. Here the id is the name of the URL Param.
So,
How can I locally invoke all node.js functions, instead of one by one?
How can I make sure that functions with URL Params also can be locally invoked?
In Node.JS, it is not possible to locally invoke all at once. sam local invoke FUNCTION_NAME is the way to go.
Since the question is about a REST API, if you want to check how thE rest api is working, all you have to do is this
sam build
sam local start-api
I normally run the following without build since it helps to view the changes during development:
sam local start-api 2>&1 | tr “\r” “\n”

Firebase function not fetching config variable

I'm trying to write a firebase function to send a transactional e-mail with SendGrid whenever it gets triggered by a HTTP request, but at the moment I'm having real difficulty getting the function to be able to access the configuration variables that I've set with the sendgrid key which is obviously a real problem...
I've set the config variable with the below command:
$ firebase functions:config:set sendgrid.key="KEY_HERE"
When I run firebase functions:config:get to list all the config values, it returns the correct value in the terminal:
{
"sendgrid": {
"key": "KEY_HERE"
}
}
But when I try to access it from my function (tried with both TypeScript & Vanilla JS setups), functions.config() just returns an empty object.
Has anyone encountered & solved this issue before, or am I doing something wrong? In either case, if anyone could give me a pointer then that'd be much appreciated!
Edit the first
I have deployed the function, and called it while hosted and looking at the logs, you can see the correct values. However, I still see the problem when I call the function when served locally on my machine for testing, which is a problem as I'd like to not have to deploy a function just to see if it works...

InvalidCastException: Azure Durable Functions Error

Locally testing Azure Durable Functions with VSCode + JavaScript. Able to successfully trigger the HTTP triggered Orchestration Client and can even see the request headers + body no problem. However, I receive the following error when attempting to trigger the Orchestrator:
Unable to cast object of type 'Microsoft.Azure.WebJobs.DurableOrchestrationContext' to type 'System.String'
I don't understand why DurableOrchestrationContext is trying to be turned into a string.
Code calling the Orchestrator:
context.bindings.patient = [{
FunctionName: "OrchestratorJS",
Input: req,
InstanceId: id
}];
Notes:
- I tried sending just a string as the Input, but to no effect.
- I have successfully created Durable Functions for a different project which makes this even more frustrating.
The Functions runtime is trying to cast DurableOrchestrationContext to a string because of how languages are handled in Functions v2. Unlike v1, v2 runs JavaScript functions through a Node language worker hosted in a different process from the runtime host. The language worker and the host communicate via gRPC protocol. When a function is called, the runtime host must pass bound parameter information to the function over gRPC. Parameters bound to complex objects, like DurableOrchestrationContext, must be serialized to JSON strings, passed via gRPC, and finally rehydrated for a function to consume them.
We introduced DurableOrchestrationContext to string conversion in the 1.4.0 release. Could you try updating to the latest version of the extension (1.5.0) and trying your function again?

How to query a database from an Azure function in js

I have an IOT hub getting some data. I also have an Azure function app in js that is triggered when an IOT event occurs. In the function app, I want to query the incoming data against a azure sql database.
In the azure function->application settings->connection string, I created a connection string x with value of the azure db connection string. My index.js file is as below.
module.exports = function (context, IoTHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array ${IoTHubMessages}`);
IoTHubMessages.forEach(message => {
context.log(`Processed message ${message}`);
var sqlConnection = x;
});
context.done();
};
I get an error that x is not defined. How can I access x? Also how how to execute a select query from here.
Any help will be greatly appreciated.
Try accessing your app setting using process.env["x"] Here are the docs, and here's a related issue you may face if you're trying to run this locally.
I don't have experience writing Javascript functions to execute a select query, but this documentation seems like a good place to start: Use Node.js to query Azure SQL Database
There is no easy way to access connection strings in Node.js.
Instead, the suggestion is to use app settings for all your secrets and connection strings. You can them access them using process.env.YourAppSetting.
See similar questions one and two.

Accessing API in a function in a file in node.js and then deploying that file through firebase

I am new to firebase. I am trying to create a Weather bot on Dialogflow. But, firebase doesn't seem to be able to access openweather API when index.js file is deployed. At the same time this works just fine in Command prompt.
The following error occurs while executing
https.get("https://api.openweathermap.org/data/2.5/weather?q="+city+"&APPID={APPID}",function(response){...})
Error: Firebase.child failed: First argument was an invalid path: "undefined". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
What's the problem here? How do I get around this?
function xyz(){
//Code
var https= require("https");
var city=London;
https.get("https://api.openweathermap.org/data/2.5/weather?q="+city+"&APPID={APPID}",function(response){
//Code
});
//Code
}
Google Cloud Functions doesn't allow to access Outbound networking calls to APIs other than that of Google Services in their free plan(Spark). If you want to make such call then you have to upgrade your plan. The fact that it is working on your local system is that local system allows making Outbound network calls to other services.
You can find more about pricing from here Google Pricing Plans
A small advice from my side is to use AWS Lambda if you don't want to pay and use it as free service to make Outbound networking calls.

Categories

Resources