re: How do I get an array/list of a model's current ObjectIds and/or select one at random? VIEW POST

re: Got an update! Please excuse how sloppy of code but got this to work for generating the user ObjectIds. Now on to choosing one at random and inject...

Now i'm to here. got it to successfully print into an array. now im just brainfarting on how to use that random AuthorId outside of the function:

const genRandomAuthorId = () => {
  User.find().populate("_id").exec(function(err, users) {
    const mapUsers = => (user._id._id)));
    // console.log("map of all userIds from mongodb collection: " + mapUsers) 
    let randomAuthor = mapUsers[Math.floor(Math.random() * mapUsers.length)];
    console.log("this is in the loop: " + randomAuthor)  //successfully spits out random userId I need.

console.log ("hello my id is " + genRandomAuthorId())
code of conduct - report abuse