score:-1
Accepted answer
exports.getauthorusernames = async (req, res) => {
if (req.body.data) {
let mappedarr = req.body.data.map(async nade => {
await user.findone({ _id: nade.authorid }).then(result => {
nade.author = result.username;
});
return nade;
});
res.status(200).send(await promise.all(mappedarr));
}
};
score:1
exports.getauthorusernames = async (req, res) => {
try{
if (req.body.data) {
const mappedarr = req.body.data.map(nade => user.findone({ _id: nade.authorid }));
const results = await promise.all(mappedarr);
return res.status(200).send(results);
}
} catch(e){
//handle exception here
}
};
Source: stackoverflow.com
Related Query
- How to fix async await in MERN using .map() and mongoDB call
- how to properly use the async and await keywords within a map
- How to integrate axios api in react js with async and await using react hooks
- How to use async await in React using the axios in the API call
- How to render react components by using map and join?
- Using async and await with export const
- Is possible to call async function without await keyword? and what happens if we call without await?
- How to fix 'module not found' for audio files using file-loader? Images, CSS, and JSON are working just fine
- How to make the API call and display it in React JS using React Table and Axios?
- How to fix "Failed to load resource: net::ERR_FILE_NOT_FOUND" for static images on cordova app using Reactjs and create-react-app
- How to test state update and component rerender after async call in react
- How do I map values for custom thumbnails using react-responsive-carousel and react-image-magnifiers?
- How to properly make a GET call in React returning an observable (resembling the method in Angular and not using promises)?
- How to await a setState call finishing when using useState hook that immediately needs that state to make an API call?
- How to fix "Expected an assignment or function call and instead saw an expression" in reactjs?
- how to groupby objects using lodash and map over the list -react
- How to call and use embedded assets from Contentful Rich Text fields using Gatsby?
- How to fix the type error Type '(string | Element) [] is not assignable to type 'string | Element | undefined' using react and typescript?
- How can I iterate over the list of URLs and make a call to the server using useSWR hook
- How to fix error "cannot invoke an object which is possibly undefined" using react and typescript?
- How to add a legend to the map using react leaflet, without using refs and manually modifying the DOM?
- How to draw a bubble map with longitude and latitude using Highcharts in react
- How to mock async call in React functional component using jest
- How to fix the error "Property contains doesnt exist on type 'RefObject' using typescript and react?
- How to save data as int using mongodb compass and koa js. Value automatically convert into String
- How to modify the code such that can call usehook using react and typescript?
- How do I test this async method call in reactjs using mocha
- How to fix 'React: Expected an assignment or function call and instead saw an expression'( state assignment)?
- how to test button that call submit form using jest and react testing library
- How to call a function and pass parameter in from a map function
More Query from same tag
- Viewing video feed from WebRTC in react
- Previous state rendering in Chat thread React JS
- How to update the comment of my post in React
- findDOMNode error on React material-ui select menu
- Handling auth with React Hooks
- How to use keyboard shortcuts globally
- How do I access multiple state in parent from child component in React Native
- Cannot remove Material-ui TextField margin
- Typescript not allowing me to pass props to child component
- React Js / using setTimout on jsx with state
- How can I get selected data list and structure the data list
- Can we cancel out api response when the react router is changed?
- Trigger props.onChange() when onBlur() on TextField
- Not able to update form data in React
- React js : change css class based on condition
- Applying type: 'dark' to a MUI palette breaks my site unless it is defined directly in the createMuiTheme() function
- Javascript setInterval not detecting global variable change (React Native)
- How to redirect to dashboard after login in ReactJS?
- Functions are not valid as a React child. The app runs correctly but getting the warning anyway
- Async function auto return
- Redirect unlogged guest
- Webpack not rendering any markup logic on build
- Scroll to the top of the page after render in react.js
- How to insert a tag in a string to replace a letter and to customize it?
- Update input value from mobx store with debounce
- Getting specific object property values from multiple arrays
- how to rerender a component when hash change and change the state
- How to get the ref from a Mobiscroll child component into the parent
- Solved ReactJS+Redux this.props.action is not a function
- React promise code need to change a json value