如何让 Meteor.user() 在服务器端返回? [英] How to get Meteor.user() to return on the server side?
问题描述
在名为/server/main.js 的文件中(以确保最后加载).
in a file called /server/main.js (in order to ensure it is loaded last).
console.dir(Meteor.user());
抛出:
Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
所以我尝试在同一个文件中使用:
So I try to use, in the same file:
console.dir(this.userId);
返回:
undefined
所以,不要放弃,我在想没关系,我只需从标题中的 cookie 中读取即可":
so, not giving up, I'm thinking "that's fine I'll just read from the cookies in the header":
var connect = Npm.require('connect');
__meteor_bootstrap__.app.use(connect.query()).use(function(req, res, next) {
console.dir(req.headers);
next();
});
.... 除了 'cookie: 'uvf=1'' 之外,在 cookie 方面不返回任何内容
.... returns nothing in terms of cookies except for 'cookie: 'uvf=1''
我不知道该得出什么结论 - 这是毫无意义的,因为我可以否则使用 Meteor.Account 框架就好了,读取/设置用户属性等.服务器清楚地知道用户,并且清楚地知道当前用户已登录.
I'm not sure what to conclude - this is senseless as I can otherwise use the Meteor.Account framework just fine, read/set user properties, etc. The server is clearly aware of the user, and the current user clearly logged in.
我完全不知所措,任何解释/提示/指针将不胜感激.
I'm at a complete loss, any explanation / hint / pointer would be greatly appreciated.
推荐答案
必须在客户端发出请求的地方使用 Meteor.user()(例如 Meteor.methods 或 Meteor.publish).
You have to use Meteor.user() in a place where a request is made from the client (such as a Meteor.methods or a Meteor.publish).
它不能放在其他任何地方,因为meteor 不会知道用户应该绑定到的代码中的那个点.如果有一个地方从客户端发出某种形式的请求,它可以这样做:
It can't be placed anywhere else because meteor wouldn't know at that point in the code the user is supposed to bound to. If there is a place a request of some form is made from the client it can do this:
在 Meteor.publish 中:
In a Meteor.publish:
Meteor.publish("collection", function() {
//returns undefined if not logged in so check if logged in first
if(this.userId) {
var user = Meteor.users.findOne(this.userId);
//var user is the same info as would be given in Meteor.user();
}
});
在 Meteor.methods 中:
In a Meteor.methods:
Meteor.methods({
"test":function() {
//should print the user details if logged in, undefined otherwise.
console.log(Meteor.user());
}
}
在服务器端路由上使用 Meteor.user():
To use Meteor.user() on a server side route:
您需要Meteor router通过meteorite 允许您拥有服务器呈现的页面.(通过mrt install router
安装)
You need Meteor router installed as a package via meteorite to allow you to have a server rendered page. (installed via mrt install router
)
然后服务器端路由可以处理 Web 请求:
A server side route could then handle the web request:
Meteor.Router.add('/awebpage', function(id) {
var userId = this.params.userid;
var logintoken = this.params.logintoken;
var isdirect = this.param.direct;
var user = Meteor.users.findOne({_id:userId,"services.resume.loginTokens.token":logintoken});
if(user) {
//the user is successfully logged in
return "You, "+user.profile.name+", are logged in!";
}
else
{
if(isdirect) {
return "<h3>Loading</h3><script>window.location.href="/awebpage?direct=true&userid="+localStorage.getItem("Meteor.userId") +"&logintoken="+localStorage.getItem("Meteor.loginToken")</script>";
}
else
{
return "Not logged in"
}
}
});
所以现在当你访问 /awebpage
时,它会检查用户是否登录并在他们登录时做你想做的事情.最初有一个重定向来将数据从 localstorage 中继回来到 URI.
So now when you visit /awebpage
it would check whether the user is logged in and do the thing you want when they are logged in. Initially there is a redirect to relay the data from localstorage back to the URI.
这篇关于如何让 Meteor.user() 在服务器端返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!