首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么我的助手集合查询不能反映在html模板中?

为什么我的助手集合查询不能反映在html模板中?
EN

Stack Overflow用户
提问于 2018-08-08 03:43:30
回答 1查看 35关注 0票数 1

按照关于如何"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;
  }
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-08 11:06:52

当您尝试更新数据库时,问题看起来像是数据库中没有任何密钥

尝试用APIKeys.upsert替换APIKeys.update,如果不存在键,这将创建一个键。

try {
  var keyId = APIKeys.upsert( { "owner": userId }, {
    $set: {
      "key": newKey
    }
  });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51734346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档