Node.js - Pass Session Variables to a SQL Server function call - node.js

In a MS Teams BOT I have working dialog code to ask a series of questions and return the formatted response back to the user in a session like so:
session.send(`Details: <br/>Question1: ${session.dialogData.Question1} <br/>Question2: ${session.dialogData.Question2} <br/>Question3: ${session.dialogData.Question3}`);
So I know the values I need are in the session variables. Now I want to pass the session variables to an SQL insert function (executeStatement):
var session_username = session.dialogData.username
var session_yesterday = session.dialogData.yesterday
var session_today = session.dialogData.today
var session_obstacles = session.dialogData.obstacles
executeStatement(session_username, session_yesterday, session_today, session_obstacles)
I can pass strings and the function works fine. But when I try to pass the session variables like above or just passing session.dialogData.yesterday for instance, straight into the function all of the code runs fine - no errors are thrown - but the session variables are not inserted and 0 rows are returned.
What is the proper way to pass a session variable? Google has not been kind in this regard. :)
Thank you in advance...
Edit:
So I couldn't sleep and had a (weird) idea. Use the session.send when assigning the values to the variable like so:
session_username = session.send(`${session.dialogData.username}`)
or
session_username = session.send(session.dialogData.username)
It writes to the database but it is an [object Object] value. So how do I get to the actual value? Hmmm...Still looking
Edit 2:
To get at the object value I tried:
session_yesterday = session.dialogData.yesterday.text()
and
session_yesterday = session.dialogData.yesterday.value()
For the 1st attempt, .text() I received the following error:
TypeError: Cannot read property 'text' of undefined
at Array.<anonymous> (C:\Developer\dailyStatus\index.js:96:56)
at C:\Developer\dailyStatus\node_modules\botbuilder\lib\dialogs\WaterfallDialog.js:67:39
at next (C:\Developer\dailyStatus\node_modules\botbuilder\lib\dialogs\WaterfallDialog.js:92:21)
at WaterfallDialog.beforeStep (C:\Developer\dailyStatus\node_modules\botbuilder\lib\dialogs\WaterfallDialog.js:99:9)
at WaterfallDialog.doStep (C:\Developer\dailyStatus\node_modules\botbuilder\lib\dialogs\WaterfallDialog.js:61:14)
at WaterfallDialog.dialogResumed (C:\Developer\dailyStatus\node_modules\botbuilder\lib\dialogs\WaterfallDialog.js:46:14)
at Session.endDialogWithResult (C:\Developer\dailyStatus\node_modules\botbuilder\lib\Session.js:358:28)
at PromptText.Prompt.invokeIntent (C:\Developer\dailyStatus\node_modules\botbuilder\lib\dialogs\Prompt.js:331:21)
at PromptText.Prompt.replyReceived (C:\Developer\dailyStatus\node_modules\botbuilder\lib\dialogs\Prompt.js:147:18)
at Session.routeToActiveDialog (C:\Developer\dailyStatus\node_modules\botbuilder\lib\Session.js:525:24)
/ - ERROR: Cannot read property 'text' of undefined
/ - Session.endConversation()

I was stubbornly trying to access the variables because I KNEW that the information was there.
I tested going back up into the dialog results.response like below and it worked:
session.dialogData.yesterday = session_yesterday = results.response;
It honestly is still a little odd to me that I couldn't just access the variable directly but oh well - lesson learned I guess.
Thank you

Related

Cloud Function ArrayRemove not deleting array value, but function runs without error

My cloud function is not deleting value from the array it is suppose to. It gets all the values correctly as I see in my debug console, but the value in the array is still there.
My code statement is as follows:
for (const id of membersUnderStructure){
const ele = context.params.structure;
console.log(`Deleting ${ele} From ${id}`);
await db.collection(`TeamMember`)
.doc(id)
.update({teamStructIds: admin.firestore.FieldValue.arrayRemove(ele)})
I know the problem is with the admin.firestore.... line as if I put a constant value there, it does update fine. My console shows the correct value of 'ele' and 'id' as well, so the values are correct. The function executes 'ok' without any error but does NOT delete the value of 'ele' from the 'teamStructIds' array.
I am not sure what am I doing wrong here?
Solution:
The Array is of 'Number' and it is trying to delete a String of 'ele'.
I just changed to parseInt(context.parama.structure) and it works now.

Values pass through request to other classes in node js

I am using mongoDB to store the document. In my http get request i pull the particular document record and save it request object to process on it(using middlewear). Later when there is any patch/put request come then i use the same request object and modify the paramater. But problem lies when i want to use the previous request stored values after modifying in put/patch request. Even i tried to store in different variable under request parameter, but when i change anything on req values, all request values get modified. Below is chuck of code.
req.PreviousStoreData = _.head(data); //data object
req.currentData = _.head(data); // copy data object
req.currentData.status = req.body.status;
// now if try to see the PreviousStoreData then it also got modified with latest status (modified only in currentData param)
If anything not understood well, please ping me back
Use Object.assign(), like that:
req.PreviousStoreData = {};
Object.assign(req.PreviousStoreData, _.head(data));
req.currentData = _.head(data);
req.currentData.status = req.body.status;
By typing "=" you set a reference to an object, which is the same object that currentData uses. Changing this object will cause both values to change.
Object.assign() copies all parameters from a source object (last argument) to the destination objects (not-last arguments), so you're actually duplicating an object instead of referring to it.
Object.assign() does seem to be the way to go. But the first parameter needs to be an empty object, to be the source object so that you dont affect the reference.
req.PreviousStoreData = Object.assign({}, _.head(data));
req.currentData = Object.assign({}, _.head(data));
req.currentData.status = req.body.status;
Here you are duplicating the Object, rather than assigning it the reference.

Passing String to Prepared Statement in Golang

The idea is I want to update the status and returning the id only if the status is different.
So, I have a prepared statement like this:
var theQuery = `UPDATE process SET status=$1 WHERE status!=$1 AND id=$2 RETURNING id`
Then, I called it with this:
err = statement.QueryRow("set", 12).Scan(&id)
Then there is an error appear like this.
runtime error: invalid memory address or nil pointer dereference
When I tried:
var theQuery = `UPDATE process SET status='$1' WHERE status!='$1' AND id=$2 RETURNING id`
It runs. Then, when I run it again, I am expecting to get no rows, but it still returning the id. It looked like it still updating the rows and ignored the status!='$1' part.
Thank you
So, I just find the solution. Instead of using $1 twice, the prepared statement will received 3 arguments:
var theQuery = `UPDATE process SET status=$1 WHERE status!=$2 AND id=$3 RETURNING id`
Then, I call the prepared statement like this.
status := "set"
err = statement.QueryRow(status, status, 12).Scan(&id)
I know maybe this is not the best approach to solve the problem. But it worked for me.

In nodejs, when the method is called multiple times at the same time, is the variable is shared

I want to generate the short id of number type, but when I call shortid method and output id and emaill, the contents of the output in the console are the same as the incoming variable. the gist in github
I get an answer that change 'var self = this ' to 'var self = {}', because 'this' in method equal 'global'.

firebase equalTo database query command

var ref = db.ref("main")
var usersRef = ref.child("users");
var accountIdVal = 56473;
What datatype is the equalTo query commmand using when I try to search with a variable, like below it does not return the desired result.
usersRef.orderByChild("accountID").equalTo(accountIdVal).once("value",function(snapshot){
//returns all the values in users
});
when the same thing is tried with non referenced value like below it works fine
usersRef.orderByChild("accountID").equalTo(56473).once("value",function(snapshot){
//returns the exact user
});
even when I use accountIdVal.toString() it does not seem to work, am I missing something here?
Hi thanks for the help I figured out the answer it was because I was getting the value of the variable as a Long, it was checking for a Integer and not a string so the datatypes were not matching up.

Resources