How get user name from the userId to display in a post?

how to get current login user id in php wordpress
wordpress get user name by id
wordpress get user meta
php get username of logged in user
get post meta by user id
php get current user id
wordpress get author by name
wordpress get admin id

Hi everyone I am trying to add the user's name in the post that is being created by that user but I'm running into trouble.

This is the part of the post where the user's name should be displayed

<Link style={{ textDecoration: "none", color: "black" }}>
   <h4
     onClick={() => this.handleShowUserProfile(event.userId)}
     className="host-name"
   >
    {getUser(event.userId).name}
   </h4>
</Link>

This is where the user is being grabbed from the database

import http from "./httpService";

const apiEndPoint = "http://localhost:3100/api/users";

export function getUsers() {
  return http.get(apiEndPoint);
}
export function getUser(userId) {
  return http.get(apiEndPoint + "/" + userId);
}

in the backend this is how the user schema looks like

const jwt = require("jsonwebtoken");
const config = require("config");
const Joi = require("joi");
const mongoose = require("mongoose");

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    minlength: 5,
    maxlength: 50
  },
  bio: {
    type: String,
    required: true,
    minlength: 200,
    maxlength: 400
  },
  interests: {
    type: Array
  },
  email: {
    type: String,
    required: true,
    minlength: 5,
    maxlength: 255,
    unique: true
  },
  password: {
    type: String,
    required: true,
    minlength: 5,
    maxlength: 1024
  },
  isAdmin: Boolean
});

userSchema.methods.generateAuthToken = function() {
  const token = jwt.sign(
    { _id: this._id, isAdmin: this.isAdmin },
    config.get("jwtPrivateKey")
  );
  return token;
};

const User = mongoose.model("User", userSchema);
function validateUser(user) {
  const schema = {
    name: Joi.string()
      .min(5)
      .max(50)
      .required(),
    bio: Joi.string()
      .required()
      .min(200)
      .max(400),
    interests: Joi.array().required(),
    email: Joi.string()
      .min(5)
      .max(255)
      .required()
      .email(),
    password: Joi.string()
      .min(5)
      .max(255)
      .required()
  };

  return Joi.validate(user, schema);
}

module.exports.User = User;
module.exports.validate = validateUser;

this is the event schema...

const Joi = require("joi");
const mongoose = require("mongoose");
const { categorySchema } = require("./category");
const { userSchema } = require("./user");

const Event = mongoose.model(
  "Events",
  new mongoose.Schema({
    image: {
      type: String
    },
    title: {
      type: String,
      required: true,
      minlength: 5,
      maxlength: 50,
      trim: true
    },
    user: {
      type: userSchema,
      required: true
    },
    details: {
      type: String,
      required: true,
      minlength: 200,
      maxlength: 300,
      trim: true
    },
    category: {
      type: categorySchema,
      required: true
    },
    numOfAttendies: {
      type: Number,
      required: true,
      min: 3,
      max: 10000
    }
  })
);

this is the handleShowUserProfile

handleShowUserProfile = id => {
   this.setState({
     showUserProfile: true,
     shownUserID: id,
     user: getUser(id)
   });
 };

 handleHideUserProfile = () => {
   this.setState({
     showUserProfile: false
   });
 };

If I had to guess, getUser() likely returns a promise, not a value. You'll need to look into reading the name from props(state) and updating state instead of trying to do it all right there.

How do I display logged-in username IF logged-in?, How do I find my current user ID in Salesforce? When you use “Create a task” action to create a task on my side, you get the “value Created by User Created by Display Name” dynamic content, but it is null. We have post this issue to Microsoft Flow Teams, if the issue is solved, I will reply to you. You could use the Get user profile action to get the creator's display name, as below:

Try logging out that get request. See if the results it returns match up with what you're trying to get from it.

export function getUser(userId) {
  const results = http.get(apiEndPoint + "/" + userId);
  console.log("results of getUser ::: ", results)
  return results
}

How to get current user information programatically in drupal 8 , How do I find my current user ID in Drupal 8? @wesm, just use an Office 365 Users Get user profile (V2) action as in the image below using the assigned to ID, then add the Display Name from the Dynamic Content into the Create a group event action. As well as being able to select the Display Name you can also get the email address and other profile info so you can send an email etc.

The getUser function is an asynchronous function so it will return a promise in most cases you need to use state and the lifecycle methods to fetch and set the username.

8 Ways to Get User ID in WordPress, Get current user ID, find ID by username, email, first and last names, username $current_user->user_login and display name $my_post = get_post( $id ); // $id - Post ID echo $my_post->post_author; // print post author ID  Getting the Display Name The first step is deciding on which name you want to call your user. The profile section of WordPress contains a number of alternatives. You can use the “nickname”, the first name, last name, or the chosen “Display name”.

Can you update your handleShowUserProfile method like the following?

handleShowUserProfile = async (id) => {

    const user = await getUser(id);
    this.setState({
      showUserProfile: true,
      shownUserID: id,
      user: user
    });
  };

I assume your api returns data for http://localhost:3100/api/users/:userId endpoint.

After you check your api and sure that it returns data, you need to check if you send a valid userId in this line.

 onClick={() => this.handleShowUserProfile(event.userId)}

Lastly you should also change the following line since the user data is in the state.

 {getUser(event.userId).name}  => {this.state.user && this.state.user.name }

listing all posts, display name of user with each post, I have an administrator panel that displays all types of posts by all users. separated by their type. currently it displays the post name and publishe i keep getting a trying to get a property of a non-object: Post $post){ $posts = $​post->where("user_id", "=", $user->id)->get(); return view('your.view' , compact('​posts')); }. Basic Usage The get_userdata() function returns an object of the user’s data. You can echo various parts of the returned object or loop through the data to display it all.

Professional CodeIgniter, $this->session->set_flashdata('error', Sorry, your username or password is incorrect! Don't forget to add MAdmins to the list of models that get autoloaded! Simply edit the First, you check to see if there are any incoming POST data. If there  I'm relatively new to ASP.Net MVC and try to use the built-in user login functionality now. I'm able to register an user in the registration view. If I try to login with the created user this also works. I'm redirected to the master page. But I'm not able to get the UserID of the current user.

get_userdata() | Function, Username: admin. User roles: administrator. User ID: 1. You can also assign certain parts into individual variables for displaying later or in multiple places. request.user.get_username() or request.user.username, former is preferred. Django docs say: Since the User model can be swapped out, you should use this method instead of referencing the username attribute directly. P.S. For templates, use {{ user.get_username }}

Username in url instead of user id PHP - PHP, I have a public user page where it displays the most current post the top portion of my code in user.php to $_GET['username'] and $username  In Windows OS, we can find the current logged in username from windows command line. We can find user name in a batch file using %username% environment variable. We can also use whoami command.

Comments
  • can you post code for handleShowUserProfile?
  • handleShowUserProfile = id => { this.setState({ showUserProfile: true, shownUserID: id, user: getUser(id) }); }; handleHideUserProfile = () => { this.setState({ showUserProfile: false }); };
  • Promise {<pending>} proto: Promise [[PromiseStatus]]: "rejected" [[PromiseValue]]: Error: Request failed with status code 404
  • Nice! looks like you're returning a promise. You will need to use a .then() or an await (async). It also looks like your request is rejected. Once you have access to the request (not the promise), check the apiEndPoint, the request method and the userId to be sure everything is correct. I recommend logging out apiEndPoint + "/" + userId to make sure it's absolutely correct!
  • i did but the name still doesn't display
  • can you console.log(user) the user after line const user = await getUser(id);
  • it return.... createError.js:17 Uncaught (in promise) Error: Request failed with status code 404 GET localhost:3100/api/users/undefined 404 (Not Found)
  • @Sami you are sending undefined as userId, Can you post all the code in your component?
  • Here event.userId is undefined, we need to fix this first. onClick={() => this.handleShowUserProfile(event.userId)}