首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过多次迭代存储信息

问通过多次迭代存储信息
EN

Stack Overflow用户
提问于 2022-02-19 14:33:43
回答 1查看 22关注 0票数 -1

嘿,所以我对编码很陌生,我已经学习JavaScript大约一个月了。我创建了一个非常基本的密码生成器,只是为了测试我学到的所有内容,但是我想弄清楚如何能够通过Node.js中的另一个脚本来存储这些密码。目前,我正在使用存储部分创建一个具有空ID和密码变量的对象,但是我不确定如何编写代码来让脚本创建多个对象来存储这些密码。如有任何帮助,将不胜感激!)

EN

回答 1

Stack Overflow用户

发布于 2022-02-19 15:50:43

由于他提到了JS和一般的编码,我认为我可以放心地假设OP不需要任何花哨的东西,只需要存储他生成的密码,不管在哪里。

当涉及到存储东西的时候,有很多的可能性。

一个很好的起点是首先寻找在文件中存储它们。

代码语言:javascript
复制
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)
}

当然,您还需要朗读它们:

代码语言:javascript
复制
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)
}

现在,您还可以将它们存储在一个数据库中,比如MongoDB (还有许多其他的,MongoDB只是在JS堆栈中一般工作得很好),或者任何其他类型的文件,就像jfriend00在编写这篇文章时在注释中提到的那样,但是我认为在深入研究这些主题之前,您需要对JS做更多的修改。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71185941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档