I am trying to create some mongoDB script (files upload to collections, find, create new collections etc). Though get confused. When I run in console:
> use importCSV
> db.people.find().pretty()
I get documents from collection on my screen, though when I run load command
> load('e:/work/parse/script.js')
i get output
true
Here javaScript file list
db = db.getSiblingDB('importCSV');
db.people.find().pretty();
I do it for debug purpose, so I create javaScript line by line to get what I want, and I need to see step by step some commands output. If I put to javaScript file command like this
print('Print from javaScript file');
it prints to console without any problems.
Why I get "true" when run from file instead of console output, and how to get list of documents printed when run from javaScript file?
Thanks
This is an expected behaviour. You need to iterate through the cursor and print each document explicitly using a .forEach loop because you are not using the interactive shell.
db = db.getSiblingDB('importCSV');
db.people.find().forEach(function(doc) {
printjson(doc);
}
Related
I want to log stuf into a file, but like with my own logs. I tried to create a Array and just stuff all the logs into it as a own string element and each 10 seconds, it appends the stuff to the file. It actually works fine BUT i have the problem that some logs get written twice. I try to asyncrously start writing the logs to the file and then clear the array to make it ready for new logs. Unfortunately, that creates clones.
There is amazing NPMS for that's you need to save your logs in a .log file.
I use log4js.
I have json file ( I create this json file using node.js) that I read in HTML using json2html library and if the json file is present / readble then it will be uploaded in our server.
I have json2html code that reads json file and prints HTML page based on content of json file. If json file is unreadable or not present, then I want node.js / javascript to stop or exit the process so nothing gets uploaded to server. How shall I stop / exit the code from processing further if json file is not available.
I do have process.exit() in my HTML file where I read json file . It does not exit gracefully. When I do inspect elements, it errors Uncaught (in promise) ReferenceError: process is not defined. I do have npm i process even though process is global. I still explicitly define const process = require('process'); in my index.js file. Still I get error while opening my html file in browser. How to get past this error. Please refer below for my code from my html file
let responseContent = fetch('studentRecords.json')
.then(function(response) {
return response.json();
})
.catch(function(err) {
process.exit(1);
});
The problem here is that you are trying to reference process which is a node global variable from the browser in your HTML file. Because of this, it won't exist.
Based on what you've mentioned, you can leave the code block as is and instead of running process.exit(1) you could try logging out an error or wrapping it in a callback function and returning an error instead? That way it won't try to process the fetch response and it will have the option to continue in the way you see fit.
I have a node.js app. And I am logging everything using winston library. My logs are daily saved on my desktop in a file. When my logs are created, there is also an audit.json file that is created automatically with my logs. And this audit.json file is saved in my logs file as well. For every run, an audit.json file is created. I want to stop occurring this audit.json file. How can I do that? Any suggestion? When I click audit.json there is some parameters that are shown such as:
(I don't have internet on my work pc so I have to write on my phone sorry)
"keep" :
"days" : false,
"amount" :5
},
"auditLog:" C:/Users/Desktop/LogFiles/.11cjeoepdwgeudp
"files":[
"date" :"1571727049689",
"name": "path"
"hash" :"054239856656...."
audit.json is an important file used by Winston to create a map of your log files. Without it, you won't be able to use the daily-rotate feature. In order to get rid of it and still use the mentioned submodule, you'd have to modify the code of file-stream-rotator - a module used by Winston. It's responsible for creating this file. You could, for example, implement a database store that would replace the json file.
The file is actually hidden on Linux systems and many users don't even know about it. Since you're using Windows, you could modify file-stream-rotator and add the hidden attribute every time the file is created. It would be much easier to achieve than moving the file map store to a database. This module would be helpful if you decided to solve the problem in this way.
I would like my JavaScript to use the PowerShell output, which is in JSON format, so I can draw a Google data table.
I have tried using window.clipboardData.getData('Text') but it doesn't seem like the answer I'm looking for and for some reason setTimeout() isn't working anymore. I have also tried event.dataTransfer.getData('Text') but I received an error
SCRIPT5007: Unable to get property 'getData' of undefined or null reference.
I kind of don't like having to copy to the clipboard because PowerShell does not take the same amount of time to finish executing every time. Additionally, the Google portion of the code constantly responds with error Invalid JSON string even though when I manually pasted the code, the data table loaded perfectly. I'm running on IE11.
You can install a HTTP server (like Apache or Lighttpd) on your computer and dump your JSON output in a .json file in the HTTP server directory.
Then you can make an ajax HTTP request to retrieved the file everytime you need it.
Here is a gist: https://gist.github.com/973e70bde8e6a530c489
I have two scenarios. One works and one fails even though the code is exactly the same.
Take a CSV file already on the box and parse it. Works perfectly. No issues.
Take a CSV file that was just created and attempt to parse it and I receive:
ENOENT, no such file or directory '/Users/Home/dev/csv/TwFrI5vhdownload.csv
Same CSV file format and all that. Wouldn't matter anyway because the created file won't even open. It fails with the error above even though the file does exist. If I restart Node and attempt to grab that file, then it works perfectly. If I run fs.stat on the newly created file it fails.
I've tried timeouts, external callbacks, etc.. but with the dynamically created file it always fails.
What am I missing here? Is the file locked and I don't know it?
Thanks!
System:
OSX Lion
Node v0.6.7
Are you sure the file is actually created when you try to parse it?
I took a look on the gist and I guess you are downloading the file from somewhere and then parsing it. Without the whole code I can only guess, but I think that you started the download, but you haven't received a clear indication it is there and ready to be parsed.