Triggering Google Cloud function using console - node.js

I am trying to use the google cloud console to test a cloud function. Below is a snippet.
exports.requestCreated = functions.firestore
.document('users/{userId}/requests/{requestId}')
.onWrite((change, context) => {
// execute operation
});
I have tried all sorts of combination of JSON data. E.g.
{"userId":"Xl86pqOpF9T2MAn12p24OJAfYJW2","requestId":"abc1234"}
But I keep getting the following statement in logs:
Request created by {userId}
The actual userId is not being read from the JSON data in the console. Can you help?

This is not a problem with the execution of the cloud function. It's a problem with hardcoding the string.
'users/{userId}/requests/{requestId}' is a hardcoded string. Node.js will not automatically replace {userId} with the value of the variable userId.
Following this previous SO post, try something like this using template strings:
`users/${userId}/requests/${requestId}`
Please note it is surrounded by backticks (`), not single quotes (').
This assumes you already have a userId and requestId variables defined. You must restructure your cloud function like this to retrieve that data. Notice that the specific variable values must be extracted from the event variable.

Thank you, Nareddyt. The function is for Firestore, and the way it is written right now checks if a new document is created under the collection requests. I tried replacing the string as you suggested, but as you pointed out, it requires these variables to be defined. I do not quite understand how to restructure the cloud function because the syntax I have used is how event detection is suggested in the Firestore documentation. My function currently works in its entirety, but testing it is a major pain. I have to go through my mobile app and do the whole userflow to test this function. I am new to Node.js and any guidance would be appreciated.

Related

What is the proper way to return a JSON object to Alexa Smart Home or end AWS Lambda in NodeJS?

I have seen three ways to return a JSON object or end a Lambda function. My trigger is Smart Home Alexa.
I am using now is context.succeed(response_JSON);This one works for me. Even if this instructions is inside a nested function. The whole Lambda ends and return the response_JSON to Smart Home Alexa.
I have seen in other blogs that say callback(response_error,response_JSON). This one did not work for me. It did not return anything to Smart Home.
Others just uses the return response_JSON. I have not used this one.
I am using now is context.succeed(response_JSON);This one works for me. Even if this instructions is inside a nested function. The whole Lambda ends and return the response_JSON to Smart Home Alexa.
context.succeed()/fail() causes the Lambda function to terminate immediately. However, I have not seen this documented in the context object docs, so it may get deprecated in later Node versions (?).
I have seen in other blogs that say callback(response_error,response_JSON). This one did not work for me. It did not return anything to Smart Home.
This one probably doesn't work for you because by default Node.js waits for the event loop to be empty before executing the callback statement. This may be due to open network/database connection. As per the doc, set the context.callbackWaitsForEmptyEventLoop variable to false to send the response right away.
Others just uses the return response_JSON. I have not used this one.
This should be used with async handlers. Read more about async and non-async handlers here: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html

Firebase Cloud Function Log. I can't see all my data in the log

I'm using Firebase Cloud Function with Node.js. With the function console.log(req.body) I want to save in the log of Firebase the data to see it then.
The problem is that data isn't complete.
As you can see the json end with the word "curr" but it should continue.
I try to see the log from the console but the message is the same:
Can I change the max size to show in the log?
No, there's a limit to how much data that can be shown in a single line, and you're exceeding it. You could consider breaking it up in to multiple lines, but it's probably easiest if you use the local emulator to make it easier to debug your functions before you deploy them.
The console.log() cannot display messages that are too long, or contain certain classes (which are then just shown as [somthing Object])
Use functions.logger.log() instead.

router.get(), use only part of url?

Currently I have this code:
router.get('/admins', function(res,req) {
However, I want it to be when someone goes to say, 'localhost:5000/admins/54323', I want the node-js app to notice 'hey, they're requesting for an admins list! lets find it'. However, with the router.get() function it only works if it is exactly that, is there a way to have it so if only the start is /admins then it sets a variable for the final part?
Through more research, I found an answer. I just put /:id after the /admins bit,
you can access the rest of the string in req.params.id where .id is the same as the string written after the : sign

How do I acquire and return the date using an AWS lamba function

I am a unity developer who has been hired to make a game on the amazon alexa. It has been a mostly smooth process so far, but I now need to call the day's date into my IDE (pullstring), and was told by pullstring support that the solution is to make an endpoint api call to a lambda function that I will create and host.
I have tried finding some basic tutorials along the lines of making a very basic lambda function, but most of the videos and tutorials I've found assume I am already a web developer and know my way around node js and amazon's service environment, which I do not.
The function that I need is simple enough (I think?), as I just need to call the current date as a number and then look up that date on a json I have set up (which is working, I just need to get the actual real date instead of a dummy variable) and have written this:
let dateObj = new Date();
let day = dateObj.getDate();
but within how a lambda function works, I'm unsure how to set up the service and call this date (day) to be returned.
Does anyone have any resources that outline constructing and using a lambda function in the most basic, most essential way possible? Will the code I wrote serve the purpose I need?
I'm a little out of my element and just need a few gaps filled in before I can learn the rest on my own.
Thank you!
This Node.js/Lambda tutorial might help get you going: https://dev.to/adnanrahic/getting-started-with-aws-lambda-and-nodejs-1kcf

Netsuite: ReferenceError functionName is not defined

This is probably a stupid one but I have tried all the things I can think of. I am currently getting the below error on my client side script when I try and execute it.
Error: ReferenceError acvt_serialNumber_saveRecord is not defined
On the Script record in Netsuite I have set the saveRecord function as follows:
acvt_serialNumber_saveRecord
The code in the file is:
function acvt_serialNumber_saveRecord(){
/**do stuff */
}
I have reuploaded to code to make sure the right version was in NetSuite. I have added one character to both the script fn name and the fn name on the Script record (as a shot in the dark). I have seen in the Javascript console at runtime that the correct code is in there and I can see the exact function name (I did a ctrl+f for the "undefined" function in the code in the console to make sure spelling was all the same).
NOTHING has worked. I had this code working earlier, but the changes I made were not to this function at all.
Any help is appreciated
Another thing to check is the code that you recently changed. In particular, check for a hanging comma. IE:
var someObj = {
someProp:'somevalue',
};
The comma at the end of 'somevalue' will cause the script to fail, throwing 'undefined' errors.
Have you tried deleting the Script record in NetSuite and re-creating it?
Do you have any library included for that Client Script in netsuite ?
Provide a screen shot of your Netsuite script page
I encounter similar problem like this before, but it was because i called a function which is inside a library file
Thanks

Resources