如何在nodejs中链滤波器和映射方法?

发布于 2025-01-20 21:32:04 字数 928 浏览 1 评论 0原文

因此,我正在研究一个项目,在该项目中,我正在致电数据库以检索存储在此处的数据。此数据是一个数组。这是代码:

  const allLogins = await Login.find().sort("name");

  const token = req.header("x-auth-token");

  const user = jwt.verify(token, config.get("jwtPrivateKey"));

  const logins = allLogins
    .filter((login) => login.userId === user._id)
    .map((login) => {
      login.password = decrypt(login.password);
    });

如果我在解密后调用Console.log,我会发现它已正确完成。我遇到的问题是,如果我console.log(登录),它说这是两个不确定的项目的数组。相反,如果我这样运行...

  const allLogins = await Login.find().sort("name");

  const token = req.header("x-auth-token");

  const user = jwt.verify(token, config.get("jwtPrivateKey"));

  let logins = allLogins.filter((login) => login.userId === user._id);
    
  logins.map((login) => {
      login.password = decrypt(login.password);
    });

那么它可以按照应有的方式工作。我不确定为什么第一组代码不起作用,以及为什么第二组确实有效。

任何帮助将不胜感激!

So I'm working on a project where I'm making a call to a database to retrieve the data stored there. This data comes as an array. here is the code:

  const allLogins = await Login.find().sort("name");

  const token = req.header("x-auth-token");

  const user = jwt.verify(token, config.get("jwtPrivateKey"));

  const logins = allLogins
    .filter((login) => login.userId === user._id)
    .map((login) => {
      login.password = decrypt(login.password);
    });

If I call a console.log after the decrypt has been run I see that it has been completed correctly. The issue I have is if I console.log(logins) it says it is an array of two items that are both undefined. If instead I run it like this...

  const allLogins = await Login.find().sort("name");

  const token = req.header("x-auth-token");

  const user = jwt.verify(token, config.get("jwtPrivateKey"));

  let logins = allLogins.filter((login) => login.userId === user._id);
    
  logins.map((login) => {
      login.password = decrypt(login.password);
    });

Then it works as it should. I'm not sure why the first set of code doesn't work and why the second set does work.

Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2025-01-27 21:32:04

基本:

  • 数组。过滤器 - 接受回调,然后回电返回布尔值(与我们的标准匹配)
  • 拨打回返回对象

array.map-接受回调,并在第二个工作示例中

logins.map((login) => { 
   // note: logins is iterated but not assigned to logins back
   // so accessing login is working
   login.password = decrypt(login.password); // map should return data
 + return login; // if we update all code will work
});

:现在来第一个示例:

const logins = allLogins
     .filter((login) => login.userId === user._id)
     .map((login) => {
      login.password = decrypt(login.password);
      + return login; // this will fix the issue 
    });

Basic :

  • array. filter - accept a callback and call back return boolean (that match our criteria)
  • array.map - accept a callback and call back return transformed object

In the second working example:

logins.map((login) => { 
   // note: logins is iterated but not assigned to logins back
   // so accessing login is working
   login.password = decrypt(login.password); // map should return data
 + return login; // if we update all code will work
});

Now coming to first example:

const logins = allLogins
     .filter((login) => login.userId === user._id)
     .map((login) => {
      login.password = decrypt(login.password);
      + return login; // this will fix the issue 
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文