How to check if currentDocument is available - xpages

I am trying to put some SSJS code together in which I'd like to check, if currentDocument is available.
I've tried something like:
if(currentDocument != undefined)
But it just throws a runtime error.
Is it even possible to check if currentDocument is available?
Any help would be greatly appreciated.

If also a null check will throw an exception I'd put this in a try-catch block and the catch block to exit with doing nothing (or maybe just logging some information, setting some scope variables to present the user a message etc.)

You almost had it:
if( typeof currentDocument != 'undefined' )

This will also work:
if ( requestScope.get('currentDocument') != null )

Related

Adding integers with the eval function is returning an error. (Nodejs)

Please don't ask me not to use eval, this isn't going to be public anyways.
I've made a chatting website, and I have implemented a "!eval" command (admin only), whatever is after it is run. I can use "!eval '2'+'2'" (Strings added), but not "!eval 2+2." The error returned is .
I've console.logged the input to the eval, and it returned exactly what I wanted: "1+1." I've looked around for this, but it seems like no one else had this problem before. A solution (more like a debugging one) is that I tried "eval('1+1')" and returned the same error. Any thoughts? Thanks in advance.
(I forgot to add what I was expecting)
I was expecting this.
VLAZ pointed out in the comments that it must be another piece of code, which he is correct. I was encrypting the messages so it can be sent securely to the client, and it only takes a string. I added
if (typeof(results) != 'string'){
results = String(results)
}
and it seemed to work, Thanks!

PasswordSignInAsync returning Success instead of RequiresVerification

I'm trying to set up Two Factor Authentication on our app. Updated a user in AspNetUsers table and set it's TwoFactorEnabled value to 1 for testing.
While debugging, signInManager.PasswordSignInAsync return just "success", not "requires verification".
This is the line
signInStatus = await signInManager.PasswordSignInAsync(viewModel.Email, viewModel.Password, true, shouldLockout: false);
(Similar questions are generally answered as first value should be name instead of email but I don't think it's the issue. Login works correctly, for example if password is wrong it returns failure.)
I added the line below to Startup.Auth.cs too
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(10));
I'm still able to work with what I have like this (though I don't want to)
if (signInStatus == SignInStatus.Success && user.TwoFactorEnabled == true)
{
//rest of code to be written
}
but this feels too makeshift of a solution and feels prone to many future errors. I'd prefer using Identity but I can't at the moment because of this problem.
It is obvious I'm doing something wrong or missing something but I don't know what. Thanks in advance.
I found my solution by copying the SignInManager code directly into mine and stepping through it, you can learn more about that here: https://stackoverflow.com/a/52357870/550975
SignInManager return RequiresVerification if :
dbo.ASpnetUsers has for user set to true TwoFactorEnabled and EmailConfirmed and user
email should be confirmed, email not be empty or null.

DustJS logic to verify empty string

I need to write logic to check whether the value is empty or it has string. Any help on this.. i tried this following. but it doesn't work in nodejs and throwing error
{#if cond="'{notes}' != '' || '{errors}' != '' "}
display html
{/if}
The #if helper is deprecated and should not be used. However, based on the code you've given, you should probably be able to use an exists check.
{?notes}
{?errors}
{! Display HTML !}
{/errors}
{/notes}
If that doesn't work for some reason, you could use the #ne helper.
{#ne key=notes value=""}
...
{/ne}
If that still isn't good enough, you could try writing a context helper. The documentation on dustjs.com is excellent.
{?notes}
Display HTML
{:else}
{?errors}
Display HTML
{/errors}
{/notes}
should do the trick.

Where to initialize extension related data

am a newbie, trying to write some basics extension. For my extension to work i need to initialize some data, so what I did is inside my background.js i declared something like this.
localStorage["frequency"] = 1; //I want one as Default value. This line is not inside any method, its just the first line of the file background.js
Users can goto Options page and change this above variable to any value using the GUI. As soon as the user changes it in UI am updating that value.
Now the problem is to my understanding background.js reloads everytime the machine is restarted. So every time I restart my machine and open Chrome the frequency value is changed back to 1. In order to avoid this where I need to initialize this value?
You could just use a specific default key. So if frequency is not set you would try default-frequency. The default keys are then still set or defined in the background.js.
I like to do that in one step, in a function like this
function storageGet(key,defaultValue){
var item = localstorage.getItem(key);
if(item === null)return defaultValue;
else return item;
}
(According to the specification localstorage must return null if no value has been set.)
So for your case it would look something like
var f = storageGet("frequency",1);
Furthermore you might be interested in checking out the chrome.storage API. It's used similar to localstorage but provides additional functionalities which might be useful for your extension. In particular it supports to synchronize the user data across different chrome browsers.
edit I changed the if statement in regard to apsillers objection. But since the specification says it's ought to be null, I think it makes sense to check for that instead of undefined.
This is another solution:
// background.js
initializeDefaultValues();
function initializeDefaultValues() {
if (localStorage.getItem('default_values_initialized')) {
return;
}
// set default values for your variable here
localStorage.setItem('frequency', 1);
localStorage.setItem('default_values_initialized', true);
}
I think the problem lies with your syntax. To get and set your localStorage values try using this:
// to set
localStorage.setItem("frequency", 1);
// to get
localStorage.getItem("frequency");

can't into OnChar() when deal with WM_KEYDOWN in 'PreTranslateMessage' function

For some reason, I need to do something when I get WM_KEYDOWN message in PreTranslateMessage, So I do something like this:
if(WM_KEYDOWN == pMsg->message && pMsg->hwnd == GetSafeHwnd())
{
SendMessage(pMsg->message,pMsg->wParam,pMsg->lParam);
return TRUE;
}
After doing so, I find there is no way to get into OnChar function, It's bad.
Anybody knows why and how to solve this problem? Help will be greatly appreciated.
When you return TRUE from PreTranslateMessage() then the message won't be processed further. You do this for every keystroke, so none will be processed and can never generate a WM_CHAR message. In other words, you got what you ask for. It also looks like you are sending the message to yourself. Hard to suggest an alternative, the code doesn't make sense.
Why will you send the same message to yourself? If all you want to do is do some processing and let the message pass through all you need to do in the 'if' statement is do your processing and let the normal message chaining work. This way the message will dispatched for further processing and eventually reach your onChar.
so your code should be something like this:
void YourWnd::PreTranslateMessage(MSG *pMsg)
{
if(WM_KEYDOWN == pMsg->message && pMsg->hwnd == GetSafeHwnd())
{
processKeyDown();
}
return CWnd::PreTranslateMessage(pMsg);
}
Why PreTranslateMessage? What possible reason would you have to do it in PreTranslateMessage?
PreTranslateMessage as the name suggests pretlanslate messages containing hot/shortcut keys.
Using it as some kind of a bandage very frequently leads to disaster sooner or later.
Could you please tell us what you are trying to achieve? Somebody will be able to come up with better solution.

Resources