Visual Studio Code local run throws error - undefined variable is defined - javascript

So I went through a Microsoft online demo on how to do an httpTrigger in Visual Studio Code (language: JavaScript) to upload it to Azure Functions and it was successful. I then started to customize the code so it can do a certain calculation. I got the calculation to be done and I was able to print to console.
Now I'm trying to run it locally so I can pass TWO values to it, but testing it with one. I noticed that in their demo, they were able to use the line:
const name = (req.query.name || (req.body && req.body.name));
where in the body response up top, a default {"name": "Azure"} would present and I sent that value, which came up in their response. Basically, they were able to assign const name with what value was passed in the curly braces with the key value pair {"name": "whateverIputhere"}.
So I decided to try in when running locally, below is the condensed code I am having trouble with:
module.exports = async function (req) {
const var1 = req.query.var1
}
and I've tried passing a parameter in multiple ways. I thought I had to do it as {"var1": "1"} but it would tell me var1 is undefined during the running of the function locally.
Why am I not able to assign it as they did?
Possible clue: There is a sample.dat file that I thought I had to modify to just replace the default entries and it still brings up {"name": "Azure"} as the default entries not sure why.
I am at a loss and would love some help.

I was having an issue only because I was NOT including .query as an option to assign the variable. All other "issues" during the description were quickly gone. Thank you all for looking at this.

Related

Mixpanel returning a response object instead of undefined even when opted out in AVA Unit Test

I'm trying to implement an AVA Unit Test for my mixpanel implementation. To do this, I'm comparing the result of mixpanel.track() where if it returns anything, the track was successful, otherwise, it should be undefined.
I thought maybe it was that it was using a different mixpanel instance so I tried creating a named instance and ensuring that but it was to no avail. I'm also trying the same process but with Amplitude and it seems to be working fine (when I am opted out, the response fails as expected)
I have done this in my components where if
const test = mixpanel.track('event_name', {}) is successful, !!test === true but if I do mixpanel.opt_out.tracking() prior to const test = mixpanel.track('event_name', {}), then !!test === undefined.
Expected behaviour (and the observed behaviour when I use it in my components):
trackResponse === undefined
Observed behaviour:
trackResponse === { event: 'asdf',
properties:
{ '$browser': 'Safari',
'$current_url': 'about:blank',
'$browser_version': null,
'$screen_height': 0,
'$screen_width': 0,
mp_lib: 'web',
'$lib_version': '2.30.1',
time: 1572898982.142,
distinct_id: '[some_id]',
'$device_id': '[some_id]',
'$initial_referrer': '$direct',
'$initial_referring_domain': '$direct',
token: '[token]' } }
where [some_id] and [token] are some distinct values I've deleted.
I don't understand why in the AVA test, I'm receiving a response when normally a failed track() results in an undefined response. Could someone shine some light on this?
Let me know if I need to provide any additional information. Thanks.
I figured it out in case anyone else runs into this issue.
I used a debugger to step into the mixpanel.track() calls and figured out that to see if the user had opted out, mixpanel checks for a property in the localStorage and compares it to see if it's === to '0'. If this fails, it assumes the user has not opted out and carries out the track call as normal.
I guess during the AVA test, it was unable to access this property and assumed the user had not opted out. To fix it, in my call to mixpanel.init(), I added opt_out_tracking_persistence_type: 'cookie' as an option so that my opt_out call was being saved somewhere that the property could be accessed during the test.

How do you update a DynamoDB table from PROVISIONED to PAY_PER_REQUEST in Node?

I have an existing DDB table which uses BillingMode: PROVISIONED and ProvisionedThroughput:{...}. I want to change it to use BillingMode: PAY_PER_REQUEST but when I do so I get the following error:
TypeError: Cannot read property 'ReadCapacityUnits' of undefined
The parameters I'm sending to the updateTable call are:
{
"TableName": "my-table-name",
"AttributeDefinitions": [
{
"AttributeType": "S",
"AttributeName": "name"
}
],
"BillingMode": "PAY_PER_REQUEST"
}
I also tried sending a ProvisionedThroughput field with the ReadCapacityUnits and WriteCapacityUnits in but this returned:
ValidationException: One or more parameter values were invalid: ProvisionedThroughput cannot be specified when BillingMode is PAY_PER_REQUEST
... which is what I would expect from reading the docs.
Any ideas what I'm doing wrong?
It turns out that this was my mistake... to answer the question, you send exactly what I posted above, or just
{
"TableName": "my-table-name",
"BillingMode": "PAY_PER_REQUEST"
}
... the "AttributeDefinitions" is unneeded as Nadav Har'El pointed out.
The problem was that in my lambda version I was building up the object to send based on what had changed and including ProvisionedThroughput: undefined which caused the SDK to try and validate this object. I did not see the problem for a long time because console.log(JSON.stringify()) removes any keys whose value is undefined. To illustrate this another way:
$ node
> var a = {foo:1, bar:undefined}
undefined
> a
{ foo: 1, bar: undefined }
> console.log(JSON.stringify(a))
{"foo":1}
undefined
This should work for you, in https://docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/WorkingWithTables.Basics.html there is an example:
aws dynamodb update-table --table-name Music --billing-mode PAY_PER_REQUEST
The "AttributeDefinitions" part in your request seems superfluous, did you try without it?
I came to this question because I had a similar problem.
Turns out I was using CDK watchful which kind of requires PROVISIONING MODE, with readCapacity and writeCapacity defined for the dynamo table. Once I'd done that, the cdk diff, cdk synth were happy again.

ngCordova: Uncaught ReferenceError: Connection is not defined

Trying to figure out this error in my Ionic app for checking network connection. Im using ngCordova's network tools found here :
http://ngcordova.com/docs/plugins/network/
ngCordova claims that their $cordovaNetwork.isOnline() function works but I'm finding quite the opposite. $cordovaNetwork.getNetwork seems to work fine but otherwise, I am getting this error when executing console.log($cordovaNetwork.inOnline()); in the code.
I've seen answers to this issue elsewhere but none of them involve using this function. They involve using a states array or 'online/offline' events.
Can someone explain why isOnline() || isOffline() doesn't seem to work? How can I use this function accordingly without any circus tricks? I am debugging via Android. I am injecting properly and doing other things properly in the code. Any help is appreciated. Thanks.
To Solve this, I had 2 workarounds for this problem
1- In Cordova/service.
this.isOffline = function() {
if (navigator.connection && typeof Connection !== 'undefined') {
me.offline = $cordovaNetwork.isOffline();
return me.offline;
}
return me.offline;
};
2- Include a new file connection.js in index.html(first js file) with below content
var Connection = {
UNKNOWN: "unknown",
ETHERNET: "ethernet",
WIFI: "wifi",
CELL_2G: "2g",
CELL_3G: "3g",
CELL_4G: "4g",
CELL:"cellular",
NONE: "none"
};
there may be one more reason for this problem i.e ngCordova is firing up before the device is ready.
console.log($cordovaNetwork.inOnline());
It must be isOnline.You have a typo here.It is wrong copy paste or you have this in the code?

How to instantiate a WinMD file to a Windows Store App Project

Hi have an Windows Store App that need to reference a single Winmd file.
I've added it adding as a normal DLL, that's the correct way?
As my understanding I'm trying to "instantiate" it on my default.js as below:
var helloTest = new WinJS.HelloWR.HelloRT();
With that I'm able to access its methods as:
var ret = helloTest.helloRt();
The problem happens when I try to run it, it raises the following exception:
0x800a138f - JavaScript runtime error: Unable to get property 'HelloRT' of undefined or null reference
It means that my HelloWR namespace is undefined, I've been looking around google for some similar sample but I've found anything.
Let me know if anyone need more info about the content of this winmd.
For those whom faces the same problem, I just did as below:
var helloTest = new HelloWR.HelloRT;
It's not needed to put WinJS as the question at the Top.
Now I'm able to access its methods:
var ret = helloTest.helloRt();
//ret returns 'HelloRT'
If someone explain it better I will check as answer.

Code runs in shell but gives 'Can't find variable' error when run from a file

I'm using this guy's code to create a dial that displays a value fed to it.
When I copy each line to the Python shell, it runs beautifully. So I thought it's time to add more functionality to it. But the VERY SAME code that works in the shell doesn't work when run from a file. It gives a "Can't find variable" error for the variable 'showHr'
The code is:
import os
import gobject
import gtk
import pygtk
import sys
import webkit
pygtk.require('2.0')
URL = "/home/antimony/Oscillator Code/dials2-js.svg"
gobject.threads_init()
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_resizable(True)
window.connect("destroy", lambda window, event: gtk.main_quit())
web_view = webkit.WebView()
web_view.open(URL)
vbox = gtk.VBox(False, 0)
vbox.add(web_view)
window.add(vbox)
window.show_all()
hr1 = 23000/(1*500)
web_view.execute_script("showHr(%d)" % hr1)
gtk.main()
What it's essentially doing is feeding the value of hr1 to showHr which is a function in the SVG file.
The function is:
function showHr(newBpm) {
bpmTarget = newBpm;
}
I have zero clue about SVG, so I've no idea where anything's going wrong, and whether it's the fault of the SVG file or what. Why on earth can it not find 'showHr' when it could find that when run in the shell? :S
UPDATE 1
- I have the same problem with another program. this time with PyQt4. The problem is probably independent of the code, then. Is something wrong with my Python shell? If so, can anyone tell me how to fix it? -- Okay, this got fixed. I probably did something really stupid, like run a different file or something, cuz it worked when I tried it after a while. I have no idea what I was doing wrong.
However the earlier problem persists and I need help!
UPDATE 2
- It seems that that the line of code with the error is:
web_view.execute_script("showHr(%d)" % hr1)
I understand that to call a function in Javascript, you'd write it as "showHr(150);" (and indeed this call works when called within the SVG file). Even after modifying the code to now read
web_view.execute_script("showHr(%d);" % hr1)
it doesn't work. It still gives the same error:
Message: console message: undefined #0: ReferenceError: Can't find variable: showHr
Anyway to point it to that SVG file?
I couldn't tell you why, but I ran into a similar issue myself. After tinkering a bit, I found:
web_view.execute_script("'showHr(%d)'" % hr1)

Categories

Resources