Firebase functions.https.onCall returns null - javascript

I have the following firebase cloud function written in node.js that I call from my Android app.The function works fine but I get null when trying to get results in Android. I need return to my android app console.log(result.data); line, but that line called after firebase function completely done, so my Android app gets null.
index.js
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const subcheck = require('./subcheck');
admin.initializeApp();
exports.subcheck = functions.https.onCall((data, context) => {
subcheck.verifySubscription(data, context);
});
And this is my subcheck.js
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const key = require('./service-account-key.json'); // JSON key file
const {google} = require('googleapis');
const authClient = new google.auth.JWT({
email: key.client_email,
key: key.private_key,
scopes: ["https://www.googleapis.com/auth/androidpublisher"]
});
const playDeveloperApiClient = google.androidpublisher({
version: 'v3',
auth: authClient
});
//admin.initializeApp(functions.config().firebase);
exports.verifySubscription = function(data, context) {
const skuId = data.sku_id;
const purchaseToken = data.purchase_token;
const packageName = data.package_name;
return authClient.authorize()
.then(function(result) {
return playDeveloperApiClient.purchases.subscriptions.get({
packageName: packageName,
subscriptionId: skuId,
token: purchaseToken
}).then(function(result) {
if (result.status === 200) {
console.log(result.data);
return {
data: result.data,
status: 200,
message: "Verified Subscription"
};
} else {
console.log("Failed to verify subscription, Try again!");
return {
data: result.data,
status: 500,
message: "Failed to verify subscription, Try again!"
};
}
})
.catch(function(error) {
console.log(error);
});
})
.catch(function(error) {
console.log(error);
});
}
This is my firebase functions console log:
I'm trying to get results in Android with the following method :
private Task<String> checkUserSubscribed(PurchaseHistoryRecord purchase) {
Map<String, Object> mapUserPurchase = new HashMap<>();
mapUserPurchase.put("sku_id", purchase.getSku());
mapUserPurchase.put("purchase_token", purchase.getPurchaseToken());
mapUserPurchase.put("package_name", "xxxxxxx");
return mFirebaseFunctions
.getHttpsCallable("subcheck")
.call(mapUserPurchase)
.continueWith(new Continuation<HttpsCallableResult, String>() {
#Override
public String then(#NonNull Task<HttpsCallableResult> task) throws Exception {
String result = (String) task.getResult().getData();
return result;
}
}).addOnCompleteListener(new OnCompleteListener<String>() {
#Override
public void onComplete(#NonNull Task<String> task) {
String result = task.getResult();
Toast.makeText(MainActivity.this, "func results: " + result, Toast.LENGTH_SHORT).show();
}
});
}

The problem most probably comes from the fact that you don't return the result of the call to the verifySubscription() function in:
exports.subcheck = functions.https.onCall((data, context) => {
subcheck.verifySubscription(data, context);
});
You should do:
exports.subcheck = functions.https.onCall((data, context) => {
return subcheck.verifySubscription(data, context);
});
Also, you should probably re-organize your Promise chaining as follows, to avoid nesting the calls to then(), see more details/explanantions here.
exports.verifySubscription = function(data, context) {
const skuId = data.sku_id;
const purchaseToken = data.purchase_token;
const packageName = data.package_name;
return authClient.authorize()
.then(result => {
return playDeveloperApiClient.purchases.subscriptions.get({
packageName: packageName,
subscriptionId: skuId,
token: purchaseToken
})
})
.then(result => {
if (result.status === 200) {
console.log(result.data);
return {
data: result.data,
status: 200,
message: "Verified Subscription"
};
} else {
console.log("Failed to verify subscription, Try again!");
return {
data: result.data,
status: 500,
message: "Failed to verify subscription, Try again!"
};
}
})
})
.catch(function(error) {
console.log(error);
});
}
Finally, you may have a look at the documentation on error handling in a Callable Cloud Function. Instead of returning {data: result.data,status: 500, ...}; you may throw an instance of functions.https.HttpsError.

Related

Error: function uses multiple asynchronous interfaces: callback and promise

Error: function uses multiple asynchronous interfaces: callback and
promise
to use the callback interface: do not return a promise
to use the promise interface: remove the last argument to the function
I'm trying to write a cucumber test to one of my GET node API, and keep getting the above, looked at few GitHub and stack-overflow posts, and could not understand the issue, below are my test method details.
App.ts
async getSsoId(refId: any): Promise<string> {
let ssoId = '';
const secrets = await this.vaultService.getClientSecrets();
this.decrypt(refId, secrets.encryption_secret, secrets.encryption_Id).then((value: any) => {
ssoId = value;
});
return ssoId;
}
api.get('/status', async (req, res) => {
let id;
const statusCode = 200;
try {
id = await this.getId(String('123456'));
} catch (err: any) {
throw new ApiError('Error fetching id');
}
try {
const item = await dbService.getItem(id);
if (item) {
statusCode = 201;
} else {
statusCode = 202;
}
} catch (err: any) {
throw new ApiError(
'The API encountered an error while attempting to communicate with the DB service.'
);
}
res.status(statusCode).send(statusCode);
});
Step Definition:
Given('a valid customer', function () {});
When("I call the get-id api", { timeout: 2 * 5000 }, async function (val) {
util.callGetIdAPI().then((response) => {
this.setApiResponseStatus(response.status);
this.setResponseBody(JSON.stringify(response.body));
});
});
Then("the apiResponseStatus should be <apiResponseStatus>", function (status) {
assert.strictEqual(status, this.apiResponseStatus);
});
Then("the responseBody should be {string}", function (responseBody) {
assert.equal(responseBody, this.responseBody);
});
Util Function
callGetIdAPI = async () => {
const headers = {
'Content-Type': 'application/json;v=1',
Accept: 'application/json;v=1'
}
const client = await getClient('url');
const options = {
method: 'GET',
headers: headers,
version: 3
};
let response;
try {
response = await client.get('/status', options);
return {
status: response.statusCode,
body: response.body
};
} catch(error) {
return {
status: error.statusCode,
body: {
error: {
id: error.id,
message: error.message
}
}
}
}
};
I'm new to this and trying to understand how multiple Premisses and Callbacks works in parallel, any thoughts or inputs on what possibly cause the error, or am I missing anything ??

Propagate ApolloError to client

I have real hard time to get custom Apollo error on the client side.
Here is the server code:
...
const schema = makeExecutableSchema({
typeDefs: [constraintDirectiveTypeDefs, ...typeDefs],
resolvers,
schemaTransforms: [constraintDirective()],
});
const server = new ApolloServer({
schema,
dataSources,
context({ req }) {
const token = req.headers.authorization;
const user = token ? getUserFromToken(token) : '';
return { user };
},
debug: false,
formatError: (err) => {
// ToDo: Generate unique token and log error
if (err!.extensions!.code == 'INTERNAL_SERVER_ERROR') {
return new ApolloError('We are having some trouble', 'ERROR', {
token: 'uniquetoken',
});
}
return err;
},
uploads: false,
});
...
Client code:
...
const ADD_CLAIM = gql`
mutation addClaim($claim: ClaimInput!) {
addClaim(claim: $claim) {
id
}
}
`;
...
const [addClaim, { data, error }] = useMutation(ADD_CLAIM);
...
const onSubmit = async () => {
try {
debugger;
const r = await addClaim({
variables: {
input: {
id: insured.insured,
date: '20/12/2020',
...
therapy: treatment.treatments.map(treat => ({
id: treat.treatId,
...
})),
},
},
});
debugger;
console.log('r', r);
} catch (err) {
debugger;
setFormError(error ? error.message : err.message);
console.log('Error:', err);
}
};
...
if (error) {
debugger;
return <div>error</div>;
}
I expect to get the custom error : "We are having some trouble".
However, no matter what I do I got: "Response not successful: Received status code 400"
I am 100% give custom error from the server:
But I receive on client side:
Moreover, when I check network tab of Developer Tools, response I do have my error:
But I cannot access it from the code.
BTW, in the playground I see my error:
Here where are my errors :
error.networkError.result.errors
What nobody knows ?
Or
const errorLink = onError(({ graphQLErrors, networkError }) => {
debugger;
console.log(graphQLErrors);
console.log(networkError);
});
const client = new ApolloClient({
...
link: ApolloLink.from( [errorLink, ...]),
});
It works as well.
Yes, sometimes GraphQL is a nasty beast

how to get a document from firestore using a function in typescript?

I wanted to get user information from my collection using their ID to send them notifications. This are my functions in index.ts
export const sendNotifications = functions.firestore
.document('messages/{groupId1}/{groupId2}/{message}')
.onCreate((snapshot, context) =>{
console.log('Starting sendNotification Function');
const doc = snapshot.data();
console.log(doc.content);
console.log(getUserData(doc.idFrom))
return true;
});
export async function getUserData(id: string){
try {
const snapshot = await admin.firestore().collection('users').doc(id).get();
const userData = snapshot.data();
if(userData){
return userData.nickname;
}
} catch (error) {
console.log('Error getting User Information:', error);
return `NOT FOUND: ${error}`
}
}
From my deploy, I get the console log messages, the 'Starting sendNotification Function', then the actual 'doc.content' then an error for my 'getUserData(doc.idFrom)'.
Promise {
<pending>,
domain:
Domain {
domain: null,
_events: { error: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] } }
Thank you in advance!
You should call your async getUserData() function with await.
The following should do the trick (untested):
export const sendNotifications = functions.firestore
.document('messages/{groupId1}/{groupId2}/{message}')
.onCreate(async (snapshot, context) => {
try {
console.log('Starting sendNotification Function');
const doc = snapshot.data();
console.log(doc.content);
const nickname = await getUserData(doc.idFrom);
// Do something with the nickname value
return true;
} catch (error) {
// ...
}
});
async function getUserData(id: string) {
try {
const snapshot = await admin.firestore().collection('users').doc(id).get();
if (snapshot.exists) {
const userData = snapshot.data();
return userData.nickname;
} else {
//Throw an error
}
} catch (error) {
// I would suggest you throw an error
console.log('Error getting User Information:', error);
return `NOT FOUND: ${error}`;
}
}
Or, if you don't want to have the Cloud Function async, you can do as follows:
export const sendNotifications = functions.firestore
.document('messages/{groupId1}/{groupId2}/{message}')
.onCreate((snapshot, context) => {
console.log('Starting sendNotification Function');
const doc = snapshot.data();
console.log(doc.content);
return getUserData(doc.idFrom)
.then((nickname) => {
// Do something with the nickname value
return true;
})
.catch((error) => {
console.log(error);
return true;
});
});

AWS Lambda retries despite returning value

I have found out that my and my colleagues lambda doesn't return data we anticipate.
After what I have already found, we used deprecated invokeAsync which returnes only statusCode.
I have upgraded aws-sdk to the newest version and change the invocation code to use invoke with
InvocationType: 'RequestResponse'
as that should give us the returned value.
Lambdas code itself looks good, its an async function without arguments.
Im not using the callback here (3rd argument of handler), as we are doing some async stuff and it would require a small refactor to not use async/await, also i have read that just returning value is ok as well.
Earlier lambda was returning array, but after some investigation and googling I have changed it to
return {
statusCode: 200,
body: JSON.stringify(result)
}
where result is the mentioned array, as this was recurring pattern in search results.
Overall result is in our service where we do the invocation, we get timeout, lambda logs that it is returning value, but then retries itself again and again.
Dumping code
invocation in our service:
const lambda = new AWS.Lambda({ httpOptions: { timeout: 600000 } })
const results = await new Promise((resolve, reject) => {
lambda.invoke(
{
FunctionName: `lambda-name`,
InvocationType: 'RequestResponse',
Payload: '""'
},
(err, data) => {
if (err) {
reject(err)
} else {
resolve(data)
}
}
)
})
lambda handler
import { parallelLimit } from 'async'
const PARALLEL_FETCHERS_NUMBER = 2
export async function runTasksInParallel (
tasks,
limit,
) {
return new Promise(
(
resolve,
reject,
) => {
parallelLimit(tasks, limit, (error, results) => {
if (error === null) {
resolve(results)
} else {
reject(error)
}
})
},
)
}
async function connectToDB(logger) {
const MONGO_URL = process.env.MONGO_URL || 'mongodb://localhost:27017/'
let db
logger.debug('Connecting to mongo')
try {
db = await MongoClient.connect(
MONGO_URL,
{
sslValidate: false,
}
)
} catch (error) {
logger.error('Could not connect to Mongo', { error })
throw error
}
logger.debug('Connected')
return db
}
async function fetchAndStore(
list,
sources,
logger
) {
const results = []
const tasks = sources.map((source) => async (callback) => {
const { fetchAdapter, storageAdapter } = source
try {
const { entries, timestamp } = await fetchAdapter.fetchLatest(list)
await storageAdapter.storeMany(timestamp, entries)
results.push(['fetch.success', 1, [ `fetcher:${fetchAdapter.name}` ] ])
} catch (error) {
const errorMessage = `Failed to fetch and store from adapter ${fetchAdapter.name}`
const { message, name, stack } = error
logger.error(errorMessage, { error: { message, name, stack } })
results.push(['fetch.error', 1, [ `fetcher:${fetchAdapter.name}` ] ])
}
callback && callback(null)
})
await runTasksInParallel(tasks, PARALLEL_FETCHERS_NUMBER)
return results
}
export async function handle() {
const logger = createLambdaLogger() // just a wrapper for console.log to match interface in service
logger.debug('Starting fetching data')
const db: Db = await connectToDB(logger)
const primaryCollection = db.collection(PRIMARY_COLLECTION)
const itemsColletion = db.collection(ITEMS_COLLECTION)
const secondaryCollection = db.collection(SECONDARY_PRICING_COLLECTION)
const primaryStorageAdapter = new StorageAdapter(
Promise.resolve(primaryCollection),
logger
)
const itemsStorageAdapter = new ItemsStorageAdapter(
Promise.resolve(itemsColletion),
logger
)
const secondaryStorageAdapter = new StorageAdapter(
Promise.resolve(secondaryCollection),
logger
)
const primaryFetchAdapter = new PrimaryFetchAdapter(getFetcherCredentials('PRIMARY'), logger)
const secondaryFetchAdapter = new SecondaryFetchAdapter(getFetcherCredentials('SECONDARY'), logger)
const sources = [
{ fetchAdapter: primaryFetchAdapter, storageAdapter: primaryStorageAdapter },
{ fetchAdapter: secondaryFetchAdapter, storageAdapter: secondaryStorageAdapter },
]
try {
const list = await itemsStorageAdapter.getItems()
logger.debug(`Total items to fetch ${list.length}`)
const result = await fetchAndStore(list, sources, logger)
logger.debug('Returning: ', { result })
return {
statusCode: 200,
body: JSON.stringify(result)
}
} catch (error) {
const errorMessage = 'failed to do task'
const { message, name, stack } = error
logger.error(errorMessage, { error: { message, name, stack } })
return {
statusCode: 500,
body: JSON.stringify(new Error(errorMessage))
}
} finally {
await db.close()
}
}
Edit:
I have changed the way i'm invoking the lambda to this below. Basically I tried to listen for every possible event that can say me something. Result is that i get a error event, with response message being Converting circular structure to JSON. Thing is I don't really know about which structure this is, as the value I'm returning is a two elements array of 3 elements (strings and number) arrays.
const lambda = new AWS.Lambda({
httpOptions: { timeout: 660000 },
maxRetries: 0
})
const request = lambda.invoke(
{
FunctionName: `${config.get('env')}-credit-rubric-pricing-fetching-lambda`,
InvocationType: 'RequestResponse',
Payload: '""'
},
(err, data) => {
if (err) {
reject(err)
} else {
resolve(data)
}
}
)
const results = await request
.on('send', () => {
logger.debug('Request sent to lambda')
})
.on('retry', response => {
logger.debug('Retrying.', { response })
})
.on('extractError', response => {
logger.debug('ExtractError', { response })
})
.on('extractData', response => {
logger.debug('ExtractData', { response })
})
.on('error', response => {
logger.debug('error', { response })
})
.on('succes', response => {
logger.debug('success', { response })
})
.on('complete', response => {
logger.debug('complete', { response })
})
.on('httpHeaders', (statusCode, headers, response, statusMessage) => {
logger.debug('httpHeaders', { statusCode, headers, response, statusMessage })
})
.on('httpData', (chunk, response) => {
logger.debug('httpData', { response, chunk })
})
.on('httpError', (error, response) => {
logger.debug('httpError', { response, error })
})
.on('httpDone', response => {
logger.debug('httpDone', { response })
})
.promise()

Adding a row to a google spreadsheet using google-spreadsheet node module and returning the status

I am able to successfully add a row to a google spreadsheet using the google-spreadsheet node module as follows:
const logToGoogleSpreadsheet = (userName, description, link) => {
const spreadsheetId = 'my-spreadsheet-id'
const doc = new GoogleSpreadsheet(`${spreadsheetId}`)
const clientEmail = 'my-client-email'
const privateKey = 'my-private-key'
const payload = {
client_email: clientEmail,
private_key: privateKey
}
let status = ''
doc.useServiceAccountAuth(payload, function (err) {
doc.addRow(1, { 'Reported By': userName, 'Description': description, 'Screenshot Link': link, 'Status': 'Open' }, function(err) {
if(err) {
console.log(err);
status = 'some error'
} else {
console.log('It worked')
status = 'success'
}
});
})
return status
}
const result = logToGoogleSpreadsheet('username', 'description', 'link')
console.log(`The value of result is ${result}`) //This always shows undefined as the value
The value of result always is 'undefined'. I know this is due to the asynchronous nature of javascript and being unable to modify anything in a callback function, but im not able to fix this issue. Can someone please show me an example of how i can return the status from the logToGoogleSpreadsheet function ?
Thank You
you could do this:
const logToGoogleSpreadsheet = *async* (userName, description, link) => {//add async keyword
const spreadsheetId = 'my-spreadsheet-id'
const doc = new GoogleSpreadsheet(`${spreadsheetId}`)
const clientEmail = 'my-client-email'
const privateKey = 'my-private-key'
const payload = {
client_email: clientEmail,
private_key: privateKey
}
let status = ''
doc.useServiceAccountAuth(payload, function (err) {
doc.addRow(1, { 'Reported By': userName, 'Description': description, 'Screenshot Link': link, 'Status': 'Open' }, function(err) {
if(err) {
console.log(err);
status = 'some error'
} else {
console.log('It worked')
status = 'success'
}
});
})
return status
}
logToGoogleSpreadsheet('username', 'description', 'link').then(res => console.log(res));
adding the async keyword to logToGoogleSpreadsheet will make it return a Promise. Now it is a 'thenable' meaning the then method can be called with the resolved result, which here is your status. So in the then call we log it.

Categories