Meteor.user() 未在客户端上显示自定义字段
Meteor.user() is not showing custom fields on the client
我在创建帐户时为我的用户添加自定义字段、发布字段和订阅该出版物,但我的 Meteor.user().customField
无法在客户端访问。
所以在我的 imports/api/users/users.js
中添加以下代码段:
import { Random } from 'meteor/random'
Accounts.onCreateUser((options, user) => {
const cond = assignUserCondition();
user.enterTime= new Date();
user.page = null;
user.passedQuiz= false;
user.exitStatus=null;
user.quizAttempts= 0;
user.condition= cond;
user.avatar= null;
user.score= 0;
user.bonus= 0;
user.lobbyTimeout= LOBBY_TIMEOUT;
user.gameId= null;
user.condInfo = {name: cond,
groupSize: (cond+'GROUPS_SIZE').split('.').reduce((o, i) => o[i], CONDITIONS_SETTINGS),
bonusConversion: (cond+'BONUS_CONVERSION').split('.').reduce((o, i) => o[i], CONDITIONS_SETTINGS),
N_ROUNDS: (cond+'N_ROUNDS').split('.').reduce((o, i) => o[i], CONDITIONS_SETTINGS),
};
return user;
});
然后,从 meteor mongo
我确认创建的用户确实有我添加的新自定义字段。现在,在 imports/api/users/server/publications.js
我有以下片段:
import {Meteor} from 'meteor/meteor'
Meteor.publish('users.user', function(currentUser) {
let user= Meteor.users.find({_id:currentUser}, {
fields: {
_id: 1,
enterTime: 1,
page: 1,
passedQuiz: 1,
exitStatus: 1,
quizAttempts:1,
condition:1,
avatar: 1,
score:1,
bonus: 1,
lobbyTimeout: 1,
gameId: 1,
conditionInfo: 1
}
});
if ( user ) {
return user;
}
return this.ready();
});
此外,在我的 imports/startup/client/index.js
我有订阅:
Tracker.autorun(function(){
Meteor.subscribe('users.user');
});
但是,在客户端,console.log(Meteor.user())
只显示 _id
和 username
而没有我的任何自定义字段。
您没有在 subscribe
中从客户端传入 currentUser
参数。然而,这是一个严格的禁忌,因为客户端不受信任(有人可以轻松地将不同用户的 _id
发送到您的方法)。将您的出版物重写为:
import {Meteor} from 'meteor/meteor'
Meteor.publish('users.user', function() {
if (this.userId) {
return Meteor.users.find(this.userId, {
fields: {
_id: 1,
enterTime: 1,
page: 1,
passedQuiz: 1,
exitStatus: 1,
quizAttempts:1,
condition:1,
avatar: 1,
score:1,
bonus: 1,
lobbyTimeout: 1,
gameId: 1,
conditionInfo: 1
}
});
}
return this.ready();
});
我在创建帐户时为我的用户添加自定义字段、发布字段和订阅该出版物,但我的 Meteor.user().customField
无法在客户端访问。
所以在我的 imports/api/users/users.js
中添加以下代码段:
import { Random } from 'meteor/random'
Accounts.onCreateUser((options, user) => {
const cond = assignUserCondition();
user.enterTime= new Date();
user.page = null;
user.passedQuiz= false;
user.exitStatus=null;
user.quizAttempts= 0;
user.condition= cond;
user.avatar= null;
user.score= 0;
user.bonus= 0;
user.lobbyTimeout= LOBBY_TIMEOUT;
user.gameId= null;
user.condInfo = {name: cond,
groupSize: (cond+'GROUPS_SIZE').split('.').reduce((o, i) => o[i], CONDITIONS_SETTINGS),
bonusConversion: (cond+'BONUS_CONVERSION').split('.').reduce((o, i) => o[i], CONDITIONS_SETTINGS),
N_ROUNDS: (cond+'N_ROUNDS').split('.').reduce((o, i) => o[i], CONDITIONS_SETTINGS),
};
return user;
});
然后,从 meteor mongo
我确认创建的用户确实有我添加的新自定义字段。现在,在 imports/api/users/server/publications.js
我有以下片段:
import {Meteor} from 'meteor/meteor'
Meteor.publish('users.user', function(currentUser) {
let user= Meteor.users.find({_id:currentUser}, {
fields: {
_id: 1,
enterTime: 1,
page: 1,
passedQuiz: 1,
exitStatus: 1,
quizAttempts:1,
condition:1,
avatar: 1,
score:1,
bonus: 1,
lobbyTimeout: 1,
gameId: 1,
conditionInfo: 1
}
});
if ( user ) {
return user;
}
return this.ready();
});
此外,在我的 imports/startup/client/index.js
我有订阅:
Tracker.autorun(function(){
Meteor.subscribe('users.user');
});
但是,在客户端,console.log(Meteor.user())
只显示 _id
和 username
而没有我的任何自定义字段。
您没有在 subscribe
中从客户端传入 currentUser
参数。然而,这是一个严格的禁忌,因为客户端不受信任(有人可以轻松地将不同用户的 _id
发送到您的方法)。将您的出版物重写为:
import {Meteor} from 'meteor/meteor'
Meteor.publish('users.user', function() {
if (this.userId) {
return Meteor.users.find(this.userId, {
fields: {
_id: 1,
enterTime: 1,
page: 1,
passedQuiz: 1,
exitStatus: 1,
quizAttempts:1,
condition:1,
avatar: 1,
score:1,
bonus: 1,
lobbyTimeout: 1,
gameId: 1,
conditionInfo: 1
}
});
}
return this.ready();
});