Why is parameter not defined? - javascript

why is dtc not defined when I call #Html.Raw(Json.Encode(dtc))? I get an error that states "The name 'dtc' does not exist in the current context"
function createChartEncounterChart(chartid, dtc) {
var mdl = #Html.Raw(Json.Encode(dtc));
...
}

You cannot use a js variable in razor view code. The #Html.Raw(...) code is run on the server and dtc is a javascript variable that won't have any value (or be valid) until it's run in the browser.

Related

How to push in 'class instance' or 'this' into a 'Function' method

i am trying to put the user defined logic (Javascript) in frontend and my backend function execute it with 'Function' method (as below)
let cmdResult = Function('"use strict"; let _AW_DAY_SUMMARY_=' + JSON.stringify(_AW_DAY_SUMMARY_) + ';' + sUserDefinedCmd)()
...
this.alicia.dump(); // run the code correctly if hardcoded in script. But hit error below when defined by user frontpage
The user defined logic work correctly, and i was able to push in object with tons of values with JSON.stringify method. But i have a challenge as i have loaded quite a number of classes
constructor(
private alicia: AliciaService,
private benny: BennyService,
private cass: CassService,
) { }
How could the user defined logic to allow call like this.alicia.dump();
Whenever i use it in my user defined logic, it complained
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'alicia' of undefined
There are many business process intensive method in those classes and i need the user defined logic to call and return rather than re-write from scratch.
The execution context of function that was created through Function constructor is a global object: window in your case.
In order to change this context you specify with each context to execute your function through Function.prototype.call():
Function('...').call(this)
Stackblitz Example

Unacaught process is not defined even when used inside an if (process) {} block? [duplicate]

This question already has answers here:
How to check a not-defined variable in JavaScript
(15 answers)
Closed 3 years ago.
So I'm trying to swap between API links in my angular app based on the origin, but it is an SSR app so I'm trying to account for an environment variable as well as window location, the code is as follows:
const getApiUrl = (): string => {
if (process && process.env?.AZURE_ENV === 'development') {
return 'devlink for SSR';
} else if (
window &&
window.location.origin === 'devclient'
) {
return 'devlink for frontendclient';
} else {
return 'link.com/';
}
};
Now the error being thrown out is:
Uncaught ReferenceError: process is not defined
I've digged into the 'compiled' script and have 100% confirmed it's coming from this piece of code.
Shouldn't this still work though?
I've also tried a vesion where I just have if(process) and get the exact same result as above.
Probably it is not there so it will fail to evaluate, maybe testing it like typeof process !== 'undefined' will help
If process has never been defined in any accessible context I think it will fail with an UncaughtReferenceError because it is trying to access something not in the context. Undefined means that the variable exists but it has no value set and this error says that the variable is just not there, thats why checkinng the type of it will probably solve the issue.
Nope. While a non-exising field of an object is really undefined, read access to a non-existing variable is an error in JavaScript, use typeof as other answers suggest...
console.log("typeof {}.process",typeof {}.process);
console.log("typeof process",typeof process);
console.log("{}.process",{}.process);
console.log("process",process);
... also, your code is TypeScript, the :string part gives it away. Which means it is compiled to strict mode, and even write access to a non-existing non-local variable would be an error.
I'm not very familiar with angular. But typically this is supposed to be client side code and it doesn't have access to node environment variables. Even if it is SSR.
If you are using webpack you could define the environment variable as a global variable on the client side:
https://webpack.js.org/plugins/define-plugin/
For your case if you don't want to use any other solutions, firstly check global , its like window but on node, so process will be stored in global.process

Dynamics CRM - JS ReferenceError "X" is not defined

I have a short bit of Javascript that sets fields to optional for certain forms.
I've checked syntax multiple times, checked the execute order in the CRM Form, and there are no references to other JS Libraries.
function getFormName() {
var formName;
formName = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();
if (formName == "EPS Plan Form") {
Xrm.Page.getAttribute("abc_plan_type").setRequiredLevel("none");
Xrm.Page.getAttribute("usi_erisa_plan").setRequiredLevel("none");
Xrm.Page.getAttribute("abc_product").setRequiredLevel("none");
}
}
The error I'm getting seems to be in reference to general syntax issues :
ReferenceError: getName is not defined
at eval (eval at RunHandlerInternal
at RunHandlerInternal
at RunHandlers
at OnScriptTagLoaded
Seems like your event trigger on the form might be configured to call a function called getName...
If you check the form Properties > Events, do you see a call to getName in the OnLoad event? Based on your function name above you'd need it to call getFormName.
And for the record, Xrm.Page is deprecated in favor of passing execution context and using executionContext.getFormContext() (except when referencing the parent page from a web resource, in which case Xrm.Page is still supported, last I heard.)

Casper JS: TypeError: 'null' is not an object

I am trying to grab the "David Welsh" text on this page: http://foster.uw.edu/faculty-research/directory/david-welsh/
When I do document.getElementById('Boundless').children[0].children[2].children[0].children[1].children[0].innerHTML in the browser console, I successfully select the text I want, but when I run my js file in Terminal, I get the error:
TypeError: 'null' is not an object (evaluating 'document.getElementById('Boundless').children')
Why is this working in my browser but not in my local js file?
casper.then(function(){
this.each(links,function(self,link){
self.thenOpen(link,function(a){
this.echo(this.getCurrentUrl());
var name = document.getElementById('Boundless').children[0].children[2].children[0].children[1].children[0].innerHTML;
casper.echo(name);
}
});
});
});
CasperJS is built on top of PhantomJS which has two contexts. The page context (casper.evaluate()) is sandboxed. It is the only way to access the DOM, you need to use it.
var name = casper.evaluate(function(){
return document.getElementById('Boundless').children[0].children[2].children[0].children[1].children[0].innerHTML;
});
casper.echo(name);
Everything that you want to use inside of it, you have to explicitly pass in. It has no access to variables defined outside.
The PhantomJS documentation also has something important to say:
Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.
Closures, functions, DOM nodes, etc. will not work!

Undefined jscript in javascript

I have created an ASP.Net application. I am using a javascript which is written in a separate file. I am using Var myvariableName ={} in javascript file.
I have included this file in MasterPage and accessing myvariableName in my aspx page.
This is working fine in Google Chrome, however, in IE 8 an unhandled exception is thrown as
myvariableName is undefined.
the error shows as;
0x800a1391 - Microsoft JScript runtime error: 'Common' is undefined
where Common is my javascript variable.
Please assist me in resolving this issue.
You're probably accessing the variable before your external script is executed.
Be sure to access your variable as soon as the document is fully loaded (i.e. $(document).ready(function(){...}); if you use jQuery) or try to find out the real execution order with some alert (that shouldn't be browser-depentent, by the way!).
If your code is already in a document.load or $(document).ready(function) you can always
handle the variable before acessing it via
if (typeof myvariableName !== "undefined") {
// do stuff
}
Some times in IE shit happens and window.load gets screwed specially when async calls are in place.
You are probably getting this error due to a missing semicolon. Change the code to this:
var myvariableName = {};
In your asp page, where you access your custom variable, wrap your code with:
var myvariableName = {};
window.onload = function(){
// your code here where you're accessing the variable
};

Categories

Resources