I have a "pseudo" database, similar to a collection in MongoDB which has a 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",
}
];
Using .find
I am trying to get all the items with fullName: null
and programId: 'e3e20d57-571d-45ab-b13a-b07d29fcf968
.
This is my code where I am trying to access the response with .then()
as .find
is an asynchronous method.
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));
But, I am unable to access the response as I get an error saying res is not defined .
ERROR.
(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.
I know this query with .find
works as when I assign the response to a const
and then console.log({ });
I see it print fine.
const response = await roles.find({
fullName: null,
programId: 'e3e20d57-571d-45ab-b13a-b07d29fcf968'
});
console.log({ response });
However, I cannot access it with .then
and res
, maybe I am making a mistake on the syntax but any help would be highly appreciated.
If you are trying to get all the items inside roles that matches the condition
fullName === null && programId === 'e3e20d57-571d-45ab-b13a-b07d29fcf968'
you can simply use filter to iterate over the array and get the filtered results.
Here's a good article:
https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d
But if you use .find()
, it will return the first value that corresponds to the passed condition. YOu can use .find()
if that's what you are after.
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);
i think its causing the error,
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));
I think, ur trying to do something like this,
.then(res =>
res.update({
programId: x._id
}, {
fullName: x.name
})
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.