Calling another function inside map function in ReactJS

is there a way to call another function inside map function and pass the value obtained from the map function to that other function ?

Say I have obtained respective user_ids from a map function, so can I call a function by passing this user_id and return back the corresponding username with that function call ?

Any help is much appreciated.

That really feels like you would be misusing the map. Maps should not have side effects.

Have you tried it? If you showed us some code we could help out.

1 Like

I am new to it. So just wondering how to proceed.

Users Model :

const mongoose = require('mongoose')

const UserSchema = new mongoose.Schema({
    name: {
        type : String,
        required : true,
    },
    email: {
        type : String,
        required : true,
        unique : true,
    },
    password : {
        type : String,
        required : true,
    },
});

const userModel = mongoose.model('users', UserSchema);
module.exports = userModel;

Posts Model :

const mongoose = require('mongoose')

const PostSchema = new mongoose.Schema({
    post_desc: {
        type: String,
        required: true,
    },
    owner: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'users',
    },
});

const postModel = mongoose.model('posts', PostSchema);
module.exports = postModel

So basically I want to display post_desc and name from both the models. But the post_desc is in Posts Model and name is in Users Model.

Wait, are you asking how to make a join query with mongoose (which you can find out about here)? I thought we were talking about Array.prototype.map.

1 Like

Hey I found the solution to it. First I used the populate method available in Mongoose and then later I displayed the resultant Objects by converting them to an Array and implementing map function onto it (client side rendering).