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

Why isn't my helper collection query not reflecting in the html template?

按照有关如何“Write an API”的教程,我似乎被卡住了,无法超越如何让生成的 API 键显示在模板中。

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

我不是 Meteor 的新手,这让它变得更加烦人!

请帮忙!

在模板代码下方找到: /client/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}}" 下的模板中没有呈现任何内容。我不明白这是为什么。

在下面找到我的助手代码:/client/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()");    
        }
     }

});

上面的帮助程序代码在控制台中呈现:Failed! Can't find: APIKeys.findOne()。 此外,当我在控制台中查询 APIKeys.find().fetch() 时,我得到了这个:

[]

在我的 onCreated 代码下面找到:/client/main.js

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

上面的 onCreated 代码在控制台中呈现:Your in onCreated!.

在下面找到我的事件代码,该代码被触发以生成新的 API 密钥:/client/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方法代码/server/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 集合没有得到更新。谁能解释为什么会这样?

我提供了更多信息,希望对您有所帮助。

在下面找到我订阅的代码/client/main.js

Router.route('/apiKey', {
 template: 'apiKey',

 waitOn: function(){
        return Meteor.subscribe('APIKey');          
    }

});

在下面找到我发布/server/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!
[]

在下面找到我声明集合的代码 imports/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;
  }
});

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

尝试将 APIKeys.update 换成 APIKeys.upsert,如果不存在,这将创建一个密钥。

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