簡體   English   中英

如何使用 Node.js 在 JSON object 中訪問 from.find 的響應

[英]How to access the response from .find in a JSON object with Node.js

我有一個“偽”數據庫,類似於 MongoDB 中的一個集合,它有一個 JSON object。

export const roles = [
  { 
    _id: "870c4350-3cf5-4f35-8429-513bd86c6734",
    programId: "e3e20d57-571d-45ab-b13a-b07d29fcf968",
    profileId: "3cbaadcf-41e1-423b-aa6a-b3fb401df148",
    fullName: null,
    jobTitle: "Regional Implementation Executive",
    department: "Electronics",
    favouriteColour: "salmon",
  },
  {
    _id: "ed231d80-f22b-4f52-bd94-9d58a2fbdbc8",
    programId: "e3e20d57-571d-45ab-b13a-b07d29fcf968",
    profileId: "1bc21aff-896f-44da-8436-a1604a626c39",
    fullName: null,
    jobTitle: "Direct Identity Strategist",
    department: "Beauty",
    favouriteColour: "silver",
  },
  {
    _id: "c6804099-150f-401b-9e3d-15945085fdde",
    programId: "e3e20d57-571d-45ab-b13a-b07d29fcf968",
    profileId: "580acb22-46f1-4bc3-b74f-606c933e43a0",
    fullName: null,
    jobTitle: "Investor Brand Developer",
    department: "Garden",
    favouriteColour: "cyan",
  },
  {
    _id: "45119579-ec48-4d7b-b4e3-656d2ad5468b",
    programId: "e3e20d57-571d-45ab-b13a-b07d29fcf968",
    profileId: "98793f3f-2f03-49a8-b77e-e96e0eff1b28",
    fullName: null,
    jobTitle: "National Research Facilitator",
    department: "Home",
    favouriteColour: "turquoise",
  }
];

使用.find我試圖獲取所有帶有fullName: nullprogramId: 'e3e20d57-571d-45ab-b13a-b07d29fcf968

這是我嘗試使用 .then .then()訪問響應的代碼,因為.find是一種異步方法。

  await roles.find({
        fullName: null,
        programId: 'e3e20d57-571d-45ab-b13a-b07d29fcf968'

      }).then((res.update(
        {
          programId: x._id,
        },
        {
          fullName: x.name,
        })
     ))
     .catch(err => console.log(err));

但是,我無法訪問響應,因為我收到錯誤說res is not defined

錯誤。

(node:99872) UnhandledPromiseRejectionWarning: ReferenceError: res is not defined
    at callback (/Users/Rahul/PersonalProjects/js-test/redact-surnames.js:28:20)
    at Object.forEach (/Users/Rahul/PersonalProjects/js-test/lib/collections.js:16:13)
    at _default (/Users/Rahul/PersonalProjects/js-test/redact-surnames.js:6:3)
(node:99872) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:99872) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我知道這個帶有.find的查詢就像我將響應分配給const然后console.log({ }); 我看到它打印得很好。

const response = await roles.find({
    fullName: null,
    programId: 'e3e20d57-571d-45ab-b13a-b07d29fcf968'
  });
  console.log({ response });

但是,我無法使用.thenres訪問它,也許我在語法上犯了錯誤,但我們將不勝感激。

如果您試圖獲取符合條件的角色內的所有項目

fullName === null && programId === 'e3e20d57-571d-45ab-b13a-b07d29fcf968'

您可以簡單地使用過濾器來遍歷數組並獲得過濾后的結果。

這是一篇好文章:

https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d

但是如果你使用.find() ,它將返回與傳遞的條件相對應的第一個值 如果這就是你所追求的,你可以使用.find()

 const roles = [ { _id: "870c4350-3cf5-4f35-8429-513bd86c6734", programId: "e3e20d57-571d-45ab-b13a-b07d29fcf968", profileId: "3cbaadcf-41e1-423b-aa6a-b3fb401df148", fullName: null, jobTitle: "Regional Implementation Executive", department: "Electronics", favouriteColour: "salmon", }, { _id: "ed231d80-f22b-4f52-bd94-9d58a2fbdbc8", programId: "e3e20d57-571d-45ab-b13a-b07d29fcf968", profileId: "1bc21aff-896f-44da-8436-a1604a626c39", fullName: null, jobTitle: "Direct Identity Strategist", department: "Beauty", favouriteColour: "silver", }, { _id: "c6804099-150f-401b-9e3d-15945085fdde", programId: "e3e20d57-571d-45ab-b13a-b07d29fcf968", profileId: "580acb22-46f1-4bc3-b74f-606c933e43a0", fullName: null, jobTitle: "Investor Brand Developer", department: "Garden", favouriteColour: "cyan", }, { _id: "45119579-ec48-4d7b-b4e3-656d2ad5468b", programId: "e3e20d57-571d-45ab-b13a-b07d29fcf968", profileId: "98793f3f-2f03-49a8-b77e-e96e0eff1b28", fullName: null, jobTitle: "National Research Facilitator", department: "Home", favouriteColour: "turquoise", } ]; // Using filter method const filterResult = roles.filter((role)=> role.fullName === null && role.programId ==='e3e20d57-571d-45ab-b13a-b07d29fcf968'); // note: result here is an array of object/s console.log(filterResult); // Using find method const findResult = roles.find((role)=> role.fullName === null && role.programId ==='e3e20d57-571d-45ab-b13a-b07d29fcf968'); // note: result here is an object console.log(findResult);

我認為它導致了錯誤,

await roles.find({
    fullName: null,
    programId: 'e3e20d57-571d-45ab-b13a-b07d29fcf968'

  }).then((res.update({  // <
    programId: x._id,    // <
  }, {                   // <
    fullName: x.name,    // <
  })))                   // <
  .catch(err => console.log(err));

我想,你試圖做這樣的事情,

.then(res =>
  res.update({
    programId: x._id
  }, {
    fullName: x.name
  })
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM