Im using Firebase cloud functions. On an iOS device, im deploying a trigger to run a cloud function in Node.Js. In Xcode - here is my client function to trigger the Cloud function - I'm passing over the dictionary data
func updateTermsOfServiceCloudCall(){
let data = [
"accountId": "acct_1Ew#######"
]
Functions.functions().httpsCallable("updateAccountWithTOA").call(data) { (result, error) in
}
Now in Node, i'm running this code to deploy to the Firebase cloud
exports.updateAccountWithTOA = functions.https.onRequest((request, response) => {
const data = request.body;
const accoundId = data.accoundId;
stripe.accounts.update(
accoundId,
{
tos_acceptance: {
date: Math.floor(Date.now() / 1000),
ip: request.ip
}
},
)
});
I'm expecting to get the dictionary data that i passed over from my iOS client. However, im having an issue getting that data in Node. I thought request.body would give me the data, but i guess im wrong because i an getting this error. Any help or suggestions would be appreciated. Thanks
Error: Stripe: Argument "id" must be a string, but got: undefined (on
API request to POST /accounts/{id})
Your client code is trying to invoke a callable function, but your function is defined as an HTTP type function. They are different things. You can't invoke a regular HTTP function using the Functions SDK.
If you want to use the Functions SDK to invoke a function, it needs to be defined with onCall rather than onRequest, as shown in the documentation for callable functions. Or, if you don't want a callable on the backend, you will need to invoke the regular HTTP function with an HTTP client library.
Related
We are using a Bot configured via Microsoft Bot Framework written in NodeJS. During the execution flow of a dialog, we present the user with certain information and then some server processing is done via SOAP and the result of this SOAP response would be needed before the next waterfall method starts.
In short, we have the below piece of code:
bot.dialog('changedefaultlogingroupDialog', [
async function (session, args, next) {
wargs[0] = 'change default login group';
var sourceFile = require('./fetchSharePointUserDetail.js');
session.privateConversationData.userSharepointEmail = global.DEVSharepointBotRequestorEmailID;
console.log('\nsession.privateConversationData.userSharepointEmail:'+session.privateConversationData.userSharepointEmail);
var get_SharepointUserId_args = ['get Sharepoint user id', session.privateConversationData.userSharepointEmail];
sourceFile.login(get_SharepointUserId_args);
setTimeout(() => {
global.DEVSharepointTeamcenterUserID = require('./fetchSharePointUserDetail.js').DEVTeamcenterUserId;
console.log('\nglobal.DEVSharepointTeamcenterUserID:'+global.DEVSharepointTeamcenterUserID+'\n');
console.log("Request has been made from directline channel by user id <"+global.DEVSharepointTeamcenterUserID+">");
session.privateConversationData.requestor_id = global.DEVSharepointTeamcenterUserID;
session.privateConversationData.create_ques = session.message.text;
next();
}, 3000);
},
async function (session, result, next) {
Do processing here that is dependent on session.privateConversationData.requestor_id
}
As you can see from the above example, the setTimeout method is waiting for 3 seconds to have the SOAP response retrieved. While this worked in DEV landscape, it failed in our PRD landscape. So I wanted to know what is the more appropriate way of doing this. Is 'await' a correct case for using in this context?. I am asking this as this is in BOT Framework Context and not sure if that has any side affects.
Please suggest.
Thanks,
Pavan.
Await is the correct way to look at this.
I'm not familiar with the bot framework, but I'm guessing that they asynchronous part of your code happens during the login.
await sourceFile.login(get_SharepointUserId_args);
Would be where the asynchronous call is. It could also be in the fetchSharePointUserDetail.js
There is likely a better way to load that file as a module so that you are calling functions on a returned object, rather than returning variables from some code that is obviously executing something.
Dart function (passing token to sendToDevice):
Future<void> _sendNotification() async {
CloudFunctions functions = CloudFunctions.instance;
HttpsCallable callable = functions.getHttpsCallable(functionName: "sendToDevice");
callable.call({
'token': await FirebaseMessaging().getToken(),
});
}
index.ts file where I have defined sendToDevice method.
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const fcm = admin.messaging();
export const sendToDevice = functions.firestore
.document('users/uid')
.onCreate(async snapshot => {
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'Dummy title',
body: `Dummy body`,
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToDevice(tokens, payload); // how to get tokens here passed from above function?
}
);
Questions:
How can I receive tokens passed from my Dart function _sendNotification to Typescript's sendToDevice function.
When I was directly passing tokens inside index.ts file, I was getting this exception:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(functionsError, Cloud function failed with exception., {code: UNAUTHENTICATED, details: null, message: UNAUTHENTICATED})
Can anyone please explain if I am supposed to authenticate something here? The command firebase login shows I am already signed in. I am very new to Typescript so please bear with these stupid questions.
Your Flutter side of code seems right, what's wrong is on the Cloud Function.
The sendToDevice function is not a callable function. It is a Cloud Firestore Triggers, it is only meant to be automatically called whenever a document matches users/{uid} is created.
Instead, you'll want to create a Callable Function, see below
export const sendToDevice = functions.https
.onCall(async (data) => {
const { token } = data; // Data is what you'd send from callable.call
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'Dummy title',
body: `Dummy body`,
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToDevice(token, payload);
}
);
You have created a database trigger, what you should do is create a callable function as shown below
exports.sendToDevice = functions.https.onCall(async (data, context) => {
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'Dummy title',
body: `Dummy body`,
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return await fcm.sendToDevice(data.token, payload);
});
There are few things to mention here:
1st The function used in 'getHttpsCallable' must be triggered by https trigger (reference here). Here we have a function triggered by firestore document create, so it won't work.
2nd You do not have parameter of your function, but you call it with parameters. If you need example of calling cloud function with parameter you can find it on pud.dev
3rd I do not have at the moment possibility to play with it, but I think that if you implement https triggered function with token parameter you should be able to pass this parameter.
I hope it will help!
UPDATE:
According to doc https triggered function has to be created with functions.https. There is a nice example in the doc. To function triggered this way you can add request body when you can pass needed data.
This answer might not solve your problem but will give you a few things to try, and you'll learn along the way. Unfortunately I wasn't able to get the callable https working with the emulator. I'll probably submit a github issue about it soon. The flutter app keeps just getting different types of undecipherable errors depending on the local URL I try.
It's good that you've fixed one of the problems: you were using document trigger (onCreate) instead of a https callable. But now, you're running a https callable and the Flutter apps needs to communicate with your functions directly. In the future, you could run the functions emulator locally, and do a lot of console.log'ing to understand if it actually gets triggered.
I have a few questions/ things you can try:
Is your user logged in the flutter app? FirebaseAuth.instance.currentUser() will tell you.
Does this problem happen on both iOS and android?
Add some logs to your typescript function, and redeploy. Read the latest logs through StackDriver or in terminal, firebase functions:log --only sendToDevice. (sendToDevice is your callable function name)
Are you deploying to the cloud and testing with the latest deployment of your functions? You can actually test with a local emulator. On Android, the url is 10.0.2.2:5001 as shown above. You also need to run adb reverse tcp:5001 tcp:5001 in the terminal. If you're on the cloud, then firebase login doesn't matter, I think your functions should already have the credentials.
To call the emulator https callable:
HttpsCallable callable = CloudFunctions.instance
.useFunctionsEmulator(origin: "http://10.0.2.2:5001")
.getHttpsCallable(functionName: "sendToDevice");
And iOS you need to follow the solution here.
One mistake I spotted. You should at least do return await fcm.sendToDevice() where you wait for the promise to resolve, because otherwise the cloud function runtime will terminate your function before it resolves. Alternatively, for debugging, instead of returning sendToDevice in your cloud function, you could have saved it into a variable, and console.log'd it. You would see its actually a promise (or a Future in dart's terminology) that hadn't actually resolved.
const messagingDevicesResponse: admin.messaging.MessagingDevicesResponse = await fcm.sendToDevice(
token,
payload
);
console.log({ messagingDevicesResponse });
return;
Make the function public
The problem is asociated with credentials. You can change the security policy of the CF and sheck if the problem is fixed. Se how to manage permisions on CF here
In the workflow of my application AppSync will delivery user inputs (as messages) to an SQS queue, that will trigger a lambda for async processing.
Lambda will validate messages against the GraphQLInputType. For that purpose, I'm using the function coerceValue of graphql-js. Got the tip from this question How can I use isValidJSValue to validate a query variable in my client code?, exactly what I needed.
The problem is that the validation code run flawlessly when running local tests within nodejs, but when deployed to AWS the very same code fails with the message:
["Expected type CreateBookInput to be an object."].
Apparently the line schema.getType(inputType) is returning only a 'string' when running on aws.
Already tried with both functions 'coerceValue' and 'isValidJSValue' but no success. Also tried to Promisify my function and use async/await style but didn't make any difference, same error.
function validate(object, inputType) {
let schema = loadSchema()
let validationResult = coerceValue(object, schema.getType(inputType))
if (validationResult.errors) {
throw new Error(`GraphQL: ${validationResult.errors}`)
}
return true
}
function loadSchema() {
let schemaFile = fs.readFileSync(path.join(__dirname, 'schema.graphql'), 'utf8'),
awsTypesSchema = fs.readFileSync(path.join(__dirname, 'aws.graphql'), 'utf8')
return buildSchema(schemaFile += awsTypesSchema)
}
When running locally I can see that schema.getType(inputType) return a GraphQLInputObjectType but the behavior differs on AWS Lambda, there in the logs it only return the name of input type, e.g. "CreateBookInput".
Did anyone had a similar problem? Any help is welcome.
Actually, there was nothing to do with GraphQL libs or lambda at all, the problem was because of a missing JSON.parse on SQS message body, that way I was trying to validate a 'string', not an object.
The idea is to implement a QBWC web service using Node.js which can serve multiple incoming requests in an asynchronous fashion. Currently I am looking into qbws which is a Node.js web service for QuickBooks Desktop Web Connector. Any ideas on how I can extend this to support an asynchronous architecture for the service methods?
Thanks in Advance!
The soap module supports asynchronous function calls which makes this easy to do. To use the same template as my other answer, here's how you'd do that:
var soap = require('soap');
var yourService = {
QBWebConnectorSvc: {
QBWebConnectorSvcSoap: {
serverVersion: function (args, callback) {
// serverVersion code here
callback({
serverVersionResult: { string: retVal }
});
},
clientVersion: function (args, callback) {
//clientVersion code here
callback({
clientVersionResult: { string: retVal }
});
},
// and all other service functions required by QBWC
}
}
};
There are two differences:
Each method signature has an additional callback parameter
There is no return, that's handled by callback() instead.
I don't currently have a suitable environment to test this, but I created a client to imitate QuickBooks Web Connector and it worked fine. Converting the qbws methods to asynchronous allowed it to service multiple clients simultaneously (including one legitimate QBWC client).
I'm just starting with Kong and setup a Lambda plugin on a service to try things out. The Lambda function I use had a simple method to parse the JSON body:
const getBody = (event: any): IBody => {
const body = JSON.parse(event.body)
return new Body(body)
}
So, although I was able to call the function and get a response from it, all I got was an error message similar to:
{"status":500,"message":"SyntaxError: Unexpected token u in JSON
at position 0"}
This is due the fact a Lambda request is different when invoked from the cli and when called from AWS API Gateway.
Basically event.body is only available when calling from the API Gateway, whilst when called from the cli, the correct property name is event.request_body.
So modifiying the method to the one below will allow me to receive calls both from AWS API Gateway and cli:
const getBody = (event: any): IBody => {
const body = JSON.parse(Object.is(event.request_body, undefined) ? event.body : event.request_body)
return new Body(body)
}