簡體   English   中英

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())僅顯示_idusername而沒有我的任何自定義字段。

您沒有在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();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM