代码之家  ›  专栏  ›  技术社区  ›  SirBT

为什么我的helper集合查询没有反映在html模板中?

  •  1
  • SirBT  · 技术社区  · 6 年前

    遵循关于如何 Write an API “,我似乎陷入了困境,无法超越如何将生成的API密钥显示在模板中。

    我有个问题 APIKeys.find().fetch() 在帮助程序中,它应该正确地反映在html模板中,但它没有。我花了几个小时查看我的代码,但没有注意到我的代码中的任何错误。

    我不是新来的流星,这让它更烦人!

    请帮忙!

    在模板代码下面查找: /客户端/main.html

    <template name="apiKey">
    <div class="row">
      <div class="col-xs-12 col-sm-6">
      <h4 class="page-header">Your API Key</h4>
      <p>To gain access to the Pizza API, use the following API Key. 
         Make sure to keep it super safe! <strong>If you'd like to
         generate a new key, click the "refresh" icon on the field
         below</strong>.</p>
    
      <label class="sr-only" for="apiKey">Your API Key</label>
        <div class="input-group">
        <input type="text" readonly class="form-control" id="apiKey" placeholder="API Key" value="{{apiKey}}">
        <div class="input-group-addon regenerate-api-key"><span class="glyphicon glyphicon-refresh"></span></div>
      </div>
    </div>
    

    在上面的模板中,在 value="{{apiKey}}" . 我不明白这是为什么。

    请在“我的助手”代码下查找: /客户端/main.js

    import '../imports/api/tasks.js';
    
    Template.apiKey.helpers({
    apiKey: function() {
            var apiKey = APIKeys.findOne();
    
            if ( apiKey ) {
                return apiKey.key;
                console.log("Sucessful");
            }
            else {
                console.log("Failed! Can't find: APIKeys.findOne()");    
            }
         }
    
    });
    

    上面的helper代码在控制台中呈现: Failed! Can't find: APIKeys.findOne() . 此外,当我询问 APIKeys.find().fetch() 在控制台中,我得到这个:

    []
    

    在下面找到我创建的代码: /客户端/main.js

    Template.apiKey.onCreated(function(){
      console.log("Your in onCreated!");
      this.subscribe( "APIKey" );
    });
    

    上面创建的代码在控制台中呈现了这一点: Your in onCreated! .

    请在下面查找触发生成新API密钥的“我的事件”代码: /客户端/main.js

    import '../imports/api/tasks.js';
    
    Template.apiKey.events({
    'click .regenerate-api-key': function( ){
            var userId = Meteor.userId();
            confirmRegeneration = confirm( "Are you sure? This will 
            invalidate your current key!" );
    
           if ( confirmRegeneration ) {
              Meteor.call( "regenerateApiKey", userId, function( error, response ) {
           if ( error ) {
              alert( error.reason, "danger" );
           }
           else {
               alert( "All done! You have a new API key: " +response );
               console.log("Response is: " +response);
          }
         });
        }
      }
    });
    

    上面的事件代码呈现一个弹出框,其中包含: All done! You have a new API key: 0 . 控制台还呈现: Response is: 0 .

    在下面查找 regenerateApiKey 方法代码 /服务器/main.js

    Meteor.methods({
      regenerateApiKey: function( userId ){
        check( userId, Meteor.userId() );
    
        var newKey = Random.hexString( 32 );
        console.log(">>>: " +newKey);
    
        try {
          var keyId = APIKeys.update( { "owner": userId }, {
            $set: {
              "key": newKey
            }
          });
          console.log(">>> newKey : " +keyId);
          return keyId;
    
        } catch(exception) {
            console.log("FAILED UPDATE")
          return exception;
        }
      }
    });
    

    上面的方法代码在终端中呈现以下内容:

    >>>: af3233a999308e39f9471b790e121cf5
    >>> newKey : 0
    

    我已将代码中的问题缩小到这个范围。这个 keyId 变量等于“0”表示没有更新APIKeys集合。有人能解释为什么会这样吗?

    我已经提供了更多的信息,希望能有所帮助。

    在我订阅的代码下面找到 /客户端/main.js

    Router.route('/apiKey', {
     template: 'apiKey',
    
     waitOn: function(){
            return Meteor.subscribe('APIKey');          
        }
    
    });
    

    在我发布的代码下面找到 /服务器/main.js

    Meteor.publish( 'APIKey', function(){
      var user = this.userId;
      var data = APIKeys.find( { "owner": user }, {fields: { "key": 1 } } );
    
      if ( data ) {
            console.log("User: " +user+ " data is: " +data);
            console.log("Was able to find data in Publish APIKey!");
            console.log(APIKeys.find().fetch());
        return data;
      }
      return this.ready();
    });
    

    上面的发布代码在终端中呈现以下内容:

    User: xELzNtMQp7u9FpZib data is: [object Object]
    Was able to find data in Publish APIKey!
    []
    

    在下面找到我声明集合的代码 导入/api/tasks.js

    import { Mongo } from "meteor/mongo";
    import { Template } from 'meteor/templating';
    import { ReactiveVar } from 'meteor/reactive-var';
    
    global.APIKeys = new Meteor.Collection("apiKeys");
    
    
    /*
    * Allow
    */
    
    APIKeys.allow({
      insert: function(){
        // Disallow inserts on the client by default.
        return false;
      },
      update: function(){
        // Disallow updates on the client by default.
        return false;
      },
      remove: function(){
        // Disallow removes on the client by default.
        return false;
      }
    });
    
    /*
    * Deny
    */
    
    APIKeys.deny({
      insert: function(){
        // Deny inserts on the client by default.
        return true;
      },
      update: function(){
        // Deny updates on the client by default.
        return true;
      },
      remove: function(){
        // Deny removes on the client by default.
        return true;
      }
    });
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   coagmano    6 年前

    当您尝试更新数据库时,问题似乎是数据库中没有任何键

    试着换掉 APIKeys.update 对于 APIKeys.upsert 如果一个密钥不存在,它将创建密钥。

    try {
      var keyId = APIKeys.upsert( { "owner": userId }, {
        $set: {
          "key": newKey
        }
      });