The `.mongorc.js` is not found, but there is one - javascript

When I start the mongo client mongo.exe, it prints out the following error:
MongoDB shell version: 2.4.9
connecting to: test
Wed Mar 12 11:15:47.013 In File::open(), CreateFileW for 'C:\Users\Zolt�n\.mongo
rc.js' failed with errno:3 The system cannot find the path specified.
Wed Mar 12 11:15:47.014 In File::len(), GetFileSizeEx for 'C:\Users\Zolt�n\.mong
orc.js' failed with errno:6 The handle is invalid.
Wed Mar 12 11:15:47.015 In File::read(), SetFilePointerEx for 'C:\Users\Zolt�n\.
mongorc.js' tried to set the file pointer to 0 but failed with errno:6 The handl
e is invalid.
>
As you see, my user path is C:\User\Zoltán and the character á is read as an unknown character. I bet this is the problem, why mongo can't find my .mongorc.js.
Is there any way I can explicitly set the path for .mongorc.js. Maybe with a configuration file?

mongo --norc ignores the file, and you're good to go.

Not yet there isn't. There will be a "global" mongorc in future versions. For now you can "fake it" like so:
mongo --norc --shell fake.js
So you tell the shell to ignore looking for .mongorc.js then you pass in a javascript file just like you would if you wanted to run the contents.
The --shell parameter forces the return to "shell" after the file ("fake.js") has been evaluated.
So just put any valid JavaScript in that file and it will be treated like the entries in the standard .mongorc.js.

I was getting the following error:
2017-10-02T12:54:21.435-0500 I STORAGE In File::open(), CreateFileW for 'H:\.mongorc.js' failed with errno:3 The system cannot find the path specified.
I created directories /data/db under the location I was getting the error and it resolved the issue.
H:\>md data
H:\>cd data
H:\data>md db
I was able to connect without any issue once those directories were created.
H:\data>mongo
MongoDB shell version: 3.0.15
connecting to: test
> db.names.insert({'name':'Mahrukh Khan'});
WriteResult({ "nInserted" : 1 })
>

You can simply delete .mongorc.js file using sudo rm .mongorc.js and the problem would be solved.

Related

Node.js run command line (child_process?)

I have a Node.js program where I need to, on a button click, run 2 commands in the Windows command line. For example, the process I'm trying to automate by the button click would be doable manually by going to cmd and entering the following commands:
pushd \\myserver.com\folder1\folder2 //Connect to remote server folder structure
mkdir NewFolder //Create new folder in the remote folder
I've found many resources pointing that I should use 'child_process', but I'm absolutely lost when it comes to shell scripting and am having a really hard time figuring out how to do this. Here's the code I have so far:
var cp = require('child_process');
cp.exec('pushd \\\\myserver.com\\folder1\\folder2\\', { shell: '/bin/bash' }, function(err, stdout, stderr){
if(err){
console.log(err);
}
});
But this above code just returns this error (which oddly removes the '\'s from the given dir):
{ Error: Command failed: pushd \\myserver.com\folder1\folder2\
/bin/bash: line 1: pushd: \myserver.comfolder1folder2: No such file or directory
at ChildProcess.exithandler (child_process.js:281:12)
killed: false,
code: 1,
signal: null,
cmd: 'pushd \\\\myserver.com\\folder1\\folder2\\' }
/bin/bash: line 1: pushd: \myserver.comfolder1folder2: No such file or directory
I'm really lost here and would appreciate any help. Any alternative you have to child_process may also be very helpful. Thank you!
Usually all the escape sequences used for string uses '\\' for a single backslash. It is understandable you used it here for the directory path for windows.
In JS particularly '\\' doesn't exactly work like that
'abc\
def' == 'abcdef' // true
'\a' == 'a' // true
When a '\' is not followed by a character with any special meaning, it is considered to be a LineContinuation instead.
As you can see from your error output using '\\\\myserver.com' considered '\myserver.com'. Plain workaround is to use '\\\\' for single '\' or use '/' for path separation which I'm not pretty sure if shell will execute it.
This is one of the blogs explains about it in details Link.
The shell is incorrect here:
{ shell: '/bin/bash' }
It should be:
{ shell: 'CMD.EXE' }
Because in the beginning of your post, you tell that you run 2 commands in the Windows command line, which indicates you are not using Bash, which is (usually) not installed on Windows.

How to take output of MySQL SHELL utility in file

I am running util.checkForServerUpgrade() function in JS mode in MySQL Shell utility. I need output of mentioned function in file instead of command-line.
Here is the exact syntax which I am using:
util.checkForServerUpgrade('root#localhost:3306', {"password":"abc", "targetVersion":"8.0.11", "outputFormat":"JSON", "configPath":"C:\ProgramData\MySQL\MySQL Server 5.7\my.ini"})
I have tried appending > output.json and many more but not working for me.
Execute the util.checkForServerUpgrade call from the command line when mysqlsh ist started:
mysqlsh root:****#localhost:3306 -e "util.checkForServerUpgrade('root#localhost:3306', {\"password\":\"abc\", \"targetVersion\":\"8.0.11\", \"outputFormat\":\"JSON\", \"configPath\":\"C:\ProgramData\MySQL\MySQL Server 5.7\my.ini\"})" > err.log

Nodejs - Get environment variables of running process

I am writing an extension for vscode, and I need to get the environment variables of a process that is already running. But I wasn't able to find a way to do it.
I know how to do it in python using psutil:
for proc in psutil.process_iter(attrs=['name', 'exe']):
if proc.info['name'].lower() == 'SomeProcess.exe'.lower():
return proc.environ()
Is there something similar for javascript/nodejs?
You can use child_process module to spawn a terminal and execute the following commands wrt platform and get the variables, parse & use or write a native node module to access the proper APIs of each platform and get the output.
Windows (Using powershell, 2019 is the PID )
(Get-Process -id 2019).StartInfo.EnvironmentVariables
Linux
tr '\0' '\n' < /proc/2019/environ
Mac
ps eww -o command 2019 | tr ' ' '\n'
Thanks to https://serverfault.com/a/66366 & https://stackoverflow.com/a/28193753/12167785 & https://apple.stackexchange.com/a/254253 &
https://stackoverflow.com/a/11547409/12167785 &
https://stackoverflow.com/a/18765553/12167785
Combining with #SudhakarRS's answer:
var child = require('child_process').execFile('powershell', [
'(Get-Process SomeProcess).StartInfo.EnvironmentVariables'
], function(err, stdout, stderr) {
console.log(stdout);
});
If you want to debug it, make sure you peek at err and stderr.
Replacing SomeProcess with notepad works for me, but using notepad.exe does not.
On powershell you can get the processes with a particular name using Get-Process [process name].
So, for example, if I have 4 instances of notepad running and do Get-Process notepad, I see this:
You can get the process IDs with (Get-Process notepad).Id which returns:
You could use the same code to choose the ID:
var child = require('child_process').execFile(
'powershell',
['(Get-Process notepad).Id'],
function(err, stdout, stderr) {
var ids = stdout.split("\r\n");
ids.pop(); //remove the blank string at the end
console.log(ids);
}
);
^ which returns:
If you just want to grab the first process with a name, it's:
(Get-Process notepad)[0].StartInfo.EnvironmentVariables
^ obviously replace notepad with your process name.
Easyish way(from here, you can use something like shelljs then run:
ps faux | grep 'PROCESS_NAME'
Then extract the process id(I'm just working on a regex) and then do:
cat /proc/THE_PROCESS/environ | tr '\0' '\n'
You'll get the the env vars back as a string something like:
THEVAR=1
ANOTHERVAR=2
I reckon you just split the string by '\n' but I'm checking!
I'll update this once I figure the regex. **Are you on linux/mac or windows?
UPDATE: Check https://github.com/shelljs/shx for cross platform
There is no builtin way to do that in javascript/nodejs. If you really need to do it, then the best way is to run a command in the terminal and then parse the output to construct the object that you need.
yep:
process.env will give you what you need :)
you can read some more here.
EDIT: it will give you environment variables only for the process you're in... did I misunderstood and you want varibales of another process?

How to check whether chrome browser is installed or not using java script?

My requirement is I need to check whether Chrome browser is insatlled on the client machine or not using Javascript. I have searched on the net not able to find the way out.
Please help in getting this done.
You can't do that with JavaScript, and even if you could, you shouldn't.
JavaScript on the client doesn't have access to the user's system, for very good reasons. (Think, servers with bad intentions.)
You can check if the browser is Chrome with the next code
if(!window.chrome){
//Chrome code
}else{
// Chrome block
}
You can't. Not with JavaScript. However, you can check whether the browser that is currently being used to view your webpage is Google Chrome or not.
<script type="text/javascript">
if(window.chrome){
document.write("Browser is Chrome");
}
else{
document.write("Please download Chrome");
}
</script>
You can't get that kind of information directly from javascript.
What you can do is use that PowerShell command in a script and save the result in a file that you'll read later using javascript.
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, InstallLocation, Publisher, InstallDate | Format-Table -AutoSize
This will get you all the installed programs on the machine from the HKEY_LOCAL_MACHINE registry folder.
The exact path to the folder from wich the informations are retrieved is : HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
The given command will display you the application name followed by it's version, it's install location, publisher name and installation date in a PowerShell terminal.
If you want to output that list in a file simply add >FileName.txt after the command before pressing enter.
Note that by default the file will be created in the C:\Users\YourUserName\ folder so if you want the file to be created in a specific location you'll have to use the CD command to get to that specific location before executing the Get-Item-Property command.
This will get you done for the get installed programs on a machine part.
Now we can get into the check if app x is installed on the machine part.
First load the previously generated file in your js application you will use it's content to determine if an application is installed on the computer.
The faster way to get if 'chrome' is installed will be to load the file as a string and then do that basic stuff :
if (string.includes('chrome') == true) {
// chrome is installed on the machine
// you can do some more stuff
// like extracting it's path from the file content
} else {
console.log('error: chrome is not installed on this computer');
}
Needless to say that this will only work if used on the same computer from which you want to check the installed applications.
Edit: If you want a more practical file to use in javascript you can replace
Format-Table -AutoSize >FileName.txt
with :
Export-Csv -path .\FileName.txt -NoTypeInformation
this way you can split your file lines using the string.split(',') method and don't have to do some extra stuff to deal with the spaces between data.
Edit 2:
Here's a full working implementation that will let you retrieve informations from a PowerShell script directly from your javascript using NodeJs.
get_programs.ps1 (PowerShell script file) :
chcp 65001 # sets the encoding for displaying chars correctly
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, InstallLocation | ConvertTo-Csv -NoTypeInformation
chcp 850 # restores the default encoding set this will avoid police changes due to the terminal modifications
Notice the change at the end of the command which is now:
| ConvertTo-Csv -NoTypeInformation
this allows to log data in the PowerShell terminal in the csv format which will simplify it's parsing as a string.
If you don't want to use another file to hold those few PowerShell
commands you can use this
child = spawn("powershell.exe",[`chcp 65001
Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, InstallLocation | ConvertTo-Csv -NoTypeInformation
chcp 850`]);
as a replacement for
child = spawn("powershell.exe",["./get_programs.ps1"]);
If you choose to do this don't forget to escape the \ chars else it will not work.
app.js :
var spawn = require("child_process").spawn,child;
child = spawn("powershell.exe",["./get_programs.ps1"]); // here we start our PowerShell script "./" means that it's in the same directory as the .js file
let chromeDetails;
child.stdout.on("data", (data) => { // data event
// here we receive each outputed line in the PowerShell terminal as an Uint8Array
if (data.includes('Chrome')) { // check for the 'Chrome' string in data
chromeDetails = data.toString(); // adds data converted as string
}
});
child.stderr.on("data", (data) => { // logs errors
console.log(`Powershell Errors: ${data}`);
});
child.on("exit", () => { // exit event
console.log("Powershell Script finished");
if (chromeDetails != undefined) {
console.log(`> chrome has been detected on this computer
available informations (appName, version, installPath):
${chromeDetails}`);
} else
console.log('> chrome has not been detected on this computer');
});
child.stdin.end(); // we end the child
Expected output :
Powershell Script finished
> chrome has been detected on this computer
    available informations (appName, version, installPath):
    "Google Chrome","103.0.5060.114","C:\Program Files\Google\Chrome\Application"
If you are not on Windows you may want to take a look at Spawning .bat and .cmd files on Windows from the NodeJs documentation to get hints on how to adapt the above app.js code to work on your system.

MongoDB Shell Script on Replica Set

I'm trying to run a javascript file against a mongodb replica set but it fails on line 2 with the following error:
MongoDB shell version: 2.4.5
connecting to: test
Tue Jan 21 09:00:01.702 JavaScript execution failed:
ReferenceError: conn is not defined at /opt/mongo-scripts/views-by-date.js:L2
failed to load: /opt/mongo-scripts/views-by-date.js
This is the content of the script:
var conn = new Mongo();
var db = conn.getDB("main"); // line 2
var date = ISODate();
...
This is the command I'm running on the bash shell:
mongo /opt/mongo-scripts/views-by-date.js
I'm assuming, though may well be wrong, it has something to do with it being a replica set as the script works on my local non-replica set mongo instance.
This should do the trick:
Remove first line
Change second line to var db = db.getSisterDB("main")

Categories

Resources