The following line belongs to a 3rd party library I'm using in a Nodejs script:
if (document === undefined) return false;
source
it is raising
ReferenceError: document is not defined
I could get around it by wrapping the if statement in a try catch block:
try {
if (document === undefined) return false;
} catch (e) {
return false;
}
Is there another option that doesn't require modifying the library code?
It's a hack but I guess you just set it before you call into the library.
console.log(document === undefined) //error
global.document = undefined
console.log(document === undefined) //true
Related
I have mongoose schema
const messageSchema = new mongoose.Schema({{
name: String,
code: String //enum ['start', 'waiting', 'complete']
})
for example - I will save an item:
{
name: 'firstItem',
code: 'start'
}
next time - when update this document I want to use validation function
function validateCode(value) {
if (existed.code === 'start' && value === 'waiting') {
return true;
}
if (existed.code === 'waiting' && value === 'complete') {
return true;
}
return false;
}
but How can I call existed item id db in validate function?
Big thx!
I'm fairly certain that validators don't get access to the object that is being updated, so what you are trying to do won't work with validators.
There are however different options for you. For instance you could use a instance method.
For the validation flow you described before, a instance method accomplishing the same would be this:
messageSchema.methods.updateCode = function(value){
const fromStart = this.code === 'start' && value === 'waiting';
const fromWaiting = this.code === 'waiting' && value === 'complete';
if (fromStart || fromWaiting) {
this.code = value;
return this.save();
}
else {
throw Error("Invalid code update");
}
}
With this method you would also be free to choose a more optimized way to handle the "invalid code update" case.
More on instance methods could be found in the mongoose docs
I am developing a small page/router component on a website using Angular 7 and its CLI. At one point I need to check if the user has allowed flash, I do this by doing so:
checkFlash() {
var hasFlash = false;
try {
hasFlash = Boolean(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
} catch (exception) {
hasFlash = "undefined" != typeof navigator.mimeTypes["application/x-shockwave-flash"];
}
return hasFlash;
}
I found this off here, and it works great, but now as I am cleaning up my application I am noticing that Angular doesn't seem to like this, in fact, it says that ActiveXObject isn't defined, yet it still works.
Super confused...
I tried linking an actual flash object like so $('embed[type="application/x-shockwave-flash"]') or $('embed[type="application/x-shockwave-flash"]')[0] but had no luck, it always returned true.
I tried installing extra npm(s) including ones like activex-support and activex-data-support as well as their #types cousins. After setting them up I found out that they did nothing to help my case.
Here are the exact errors the CLI & VScode-Intellisense gave me:
VScode:
[ts] 'ActiveXObject' only refers to a type, but is being used as a value here. [2693]
any
CLI:
ERROR in src/app/games/games.component.ts(51,30): error TS2304: Cannot find name 'ActiveXObject'.
It doesn't throw this error when ran inside plain JS, but I've looked around and can't seem to figure out how to run pure JS inside Angular 2 (7). Also looked here with no luck.
Please help, completely lost here.
EDIT: Found the fix -->
The answer was here listed inside the comments (will need to make minor changes)(shown below)
change from:
checkFlash() {
var hasFlash = false;
try {
hasFlash = Boolean(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
} catch (exception) {
hasFlash = "undefined" != typeof navigator.mimeTypes["application/x-
shockwave-flash"];
}
return hasFlash;
}
to:
function checkFlash() {
var hasFlash = false;
try {
var flash =
navigator.mimeTypes &&
navigator.mimeTypes["application/x-shockwave-flash"]
? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin
: 0;
if (flash) hasFlash = true;
} catch (e) {
if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined)
hasFlash = true;
}
return hasFlash;
}
This issue with ActiveXObject can be solve as shown below:
Goto your tsconfig.json file and add the 'scripthost' library.
Then recompile your application.
"lib": [
"es2017",
"dom",
"scripthost"
]
The answer was here listed inside the comments (will need to make minor changes)(shown below)
change from:
checkFlash() {
var hasFlash = false;
try {
hasFlash = Boolean(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
} catch (exception) {
hasFlash = "undefined" != typeof navigator.mimeTypes["application/x-
shockwave-flash"];
}
return hasFlash;
}
to:
function checkFlash() {
var hasFlash = false;
try {
var flash =
navigator.mimeTypes &&
navigator.mimeTypes["application/x-shockwave-flash"]
? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin
: 0;
if (flash) hasFlash = true;
} catch (e) {
if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined)
hasFlash = true;
}
return hasFlash;
}
So I want to check if a variable is undefined in node.js. So far I work like this:
if(typeof object.data.items[1] === 'undefined')
{
break;
}
else
{
console.log("Defined");
}
But it gives me this error:
"TypeError: Cannot read property 'data' of undefined".
Any ideas on how to bypass this error while still checking if it's undefined?
check both object and object.data is defined first.
if(object && object.data && typeof object.data.items[1] === 'undefined')
{
break;
}
else
{
console.log("Defined");
}
or
if(!object || !object.data || typeof object.data.items[1] === 'undefined')
{
break;
}
else
{
console.log("Defined");
}
It seems that object variable is undefined too. Try the following code:
if(
typeof object === 'undefined' ||
typeof object.data === 'undefined' ||
typeof object.data.items[1] === 'undefined')
{
break;
}
else
{
console.log("Defined");
}
if(!_.get(object,['data','items',1]))
{
break;
}
When you try to access a property of JSON object you have to ensure that left side of your dot operator must have some value but not undefined or null. i.e.
If you want to access a.b.c.d then you have to make sure that each left side property has some value. You can use #rijin's answer.
I'd like to have my unit tests assert that a particular function call throws an AssertionError specifically when expected, rather than that it throws an exception at all. The assertion library (expect) supports such a thing by passing an exception constructor in, but I can't seem to find where, if anywhere, the AssertionError constructor is exported. Is it intended to be an internal class only and not exposed to us? The docs contain numerous references to it, but no links.
I have a super hacky way:
let AssertionError;
try {
const assert = require("assert");
assert.fail();
}
catch (ex) {
AssertionError = ex.constructor;
}
but I'm hoping there's a better way.
Assertion error class is defined here:
assert.AssertionError
*It can be useful while testing and AsserionError is expected result:
assert.throws( FunctionThatShouldThrow_AssertionError, assert.AssertionError )
After a research in the Nodejs github repo, I can tell you it is here:
https://github.com/nodejs/node/blob/c75f87cc4c8d3699e081d37bb5bf47a70d830fdb/lib/internal/errors.js
AssertionError is defined like this :
class AssertionError extends Error {
constructor(options) {
if (typeof options !== 'object' || options === null) {
throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
}
var { actual, expected, message, operator, stackStartFunction } = options;
if (message) {
super(message);
} else {
if (actual && actual.stack && actual instanceof Error)
actual = `${actual.name}: ${actual.message}`;
if (expected && expected.stack && expected instanceof Error)
expected = `${expected.name}: ${expected.message}`;
if (util === null) util = require('util');
super(`${util.inspect(actual).slice(0, 128)} ` +
`${operator} ${util.inspect(expected).slice(0, 128)}`);
}
this.generatedMessage = !message;
this.name = 'AssertionError [ERR_ASSERTION]';
this.code = 'ERR_ASSERTION';
this.actual = actual;
this.expected = expected;
this.operator = operator;
Error.captureStackTrace(this, stackStartFunction);
}
}
If I were you, I would not redefined AssertionError, that is very, very hacky. I think your best bet is to extend that class to MyAssertionError or to create a twin than extends error.
Hope that helps!
I am confused why this if statement will throw a JS error. Why isn't the function running as soon as it returns true?
res.locals.user = null;
console.info(res.locals.user === null); //true
if (res.locals.user === null && res.locals.user.level > 5) {
The && in your if statement is analogous to this:
res.locals.user = null;
console.info(res.locals.user === null); //true
if (res.locals.user === null) {
// at this point you know that res.locals.user is null
if (res.locals.user.level > 5) {
// won't get here because res.locals.user.level will throw an exception
}
}
If the first part of an && comparison evaluates to truthy, then the second part will also be evaluated since for the whole statement to be true, both pieces of the statement must be truthy.
It appears that you may want this instead:
res.locals.user = null;
console.info(res.locals.user === null); //true
if (res.locals.user === null || res.locals.user.level > 5) {
// will get here because only the first term will be evaluated
// since when the first term evaluates to true, the || is already satisfied
}
Or since I'm not quite sure which logic you want, maybe you wanted this:
res.locals.user = null;
console.info(res.locals.user === null); //true
if (res.locals.user !== null && res.locals.user.level > 5) {
// will not get here because res.locals.user doesn't pass the first test
// won't throw an exception because 2nd term won't be evaluated
}
Because the first part of the evaluation is true, so it goes on to evaluate the next part which will then always throw an exception as the first part was true. It's like a paradox :)
There are languages where the the && only executes the second comparison if the first is true (like java). However, what you wrote would fail in any language. You can't be null and level>5 all at once.