Javascript Meteor.user()未在客户端上显示自定义字段
我正在为我的用户在创建帐户时添加自定义字段,发布这些字段并订阅该出版物,但是我的Javascript Meteor.user()未在客户端上显示自定义字段,javascript,mongodb,meteor,meteor-accounts,Javascript,Mongodb,Meteor,Meteor Accounts,我正在为我的用户在创建帐户时添加自定义字段,发布这些字段并订阅该出版物,但是我的Meteor.user().customField将无法在客户端访问 因此,在我的imports/api/users/users.js中,我添加了以下代码片段: import { Random } from 'meteor/random' Accounts.onCreateUser((options, user) => { const cond = assignUserCondition();
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;
});
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();
});
然后,从meteor mongo
中,我验证了创建的用户确实具有我添加的新自定义字段。现在,在imports/api/users/server/publications.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;
});
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())
只显示\u id
和用户名
,没有任何自定义字段。您没有在订阅中从客户端传入currentUser
参数。但是,这是一个严格的“不”字,因为客户端不受信任(有人可以很容易地向您的方法发送不同用户的\u 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();
});
您没有在subscribe
中从客户端传入currentUser
参数。但是,这是一个严格的“不”字,因为客户端不受信任(有人可以很容易地向您的方法发送不同用户的\u 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();
});
好了,这就解决了,这就解决了