'Storing information through multiple iterations
Hey there so I'm pretty new to coding, I've been learning JavaScript for about a month now. I've created a very basic password generator just to test everything I've learnt, however I'm trying to figure out how I'd be able to store these passwords through another script in Node.js. I'm currently at the barest of bones with the storage part, creating an object with an empty ID and the password variable however I'm unsure how to write the code to let the script create multiple objects to store these passwords. Any help would be appreciated! :)
Solution 1:[1]
Since he mentioned being new to JS and coding in general, I think I can safely assume OP doesn't want anything fancy and just store the password(s) he generated, no matter where.
There are many possibilities when it comes to storing things.
A good place to start would be to look for storing them in a file first.
const fs = require('fs');
// say this is your passwords object
const passwds = myFunctionGeneratingPassword();
// a file 'passwords.json' will be created in your current working directory
const pathToFile = 'passwords.json';
try {
fs.writeFileSync(pathToFile, content)
//file written successfully
} catch (err) {
// if any error with the writing should occur, the error must be handled here
console.error(err)
}
Of course, you would also want to read them:
const fs = require('fs');
// the same file you created in the step before
const pathToFile = 'passwords.json';
try {
const data = fs.readFileSync(pathToFile, 'utf8');
const parsedData = JSON.parse(data);
console.log(parsedData);
} catch (err) {
// if any error with the reading should occur, the error must be handled here
console.error(err)
}
Now, you could also store them in a database such as MongoDB (there are many others, MongoDB just happens to work well in general with JS stacks), or any other kind of file as jfriend00 mentioned in the comments as I was writing this, but I think you'll need to fiddle a bit more with JS before diving into these subjects.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gaƫtan Boyals |