通过用户名或 ID 获取用户对象 JavaScript

Is there any way to get a user object by username or id via javascript?

It is difficult to answer without some more context. You can always do this: (try it in the dev tools for Meta)

Discourse.User.findByUsername("David_Taylor").then(function(user){
   console.log("User Found", user);
});

But that will result in an additional request to the server, which may not be necessary.

What are you trying to achieve?

Thanks very much. I am trying to get a list of usersnames with certain custom field attributes. As you correctly pointed out, it will result in many additional calls from the API.

Is it possible to achieve what I intend more gracefully?

If this is just for administrators, you might be able to utilise the data-explorer plugin. If you wanted this to work for all users, you would probably need to create a plugin which adds a new HTTP endpoint.

我正在处理与事件插件相关的某个 bug,当前的实现依赖于用户名。虽然这是一个边缘情况,但用户名可能会发生变化,因此需要能够通过用户的 id 来获取用户信息。

我认为核心模块中应该有一个类似的方法,用于通过 id 查找用户。

很想知道您对这一点的看法。

您的插件应能够在用户名更改时更新用户名引用,或添加必要的路由和序列化器以获取数据。

我会检查一下在当前场景下是否可以实现这一点。

是的。我需要在 UsersController 中添加一个通过 ID 查找用户的方法,并在 Ember 端的 User 模型中添加一个方法来调用这个新的控制器方法。