我在创建帐户,发布字段和订阅该出版物时为我的用户添加自定义字段,但我的BU, AUDIT, CORC, GOV, PPS, TMSC, TRAIN
Unit1, Generally Effective, Highly Effective, etc, etc
Unit2, Marginally Effective, Highly Effective, etc, etc
Unit3, ...,...,...
Unit4, ...,...,...
Unit5, ...,...,...
将无法在客户端访问。
因此,在我的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
而没有任何自定义字段。
答案 0 :(得分:5)
您没有从currentUser
中的客户端传递subscribe
参数。然而,这是严格的禁忌,因为客户端不受信任(有人可以轻松地向您的方法发送不同用户的_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();
});