javascript - 为什么我的帮助集合查询没有反映在 html 模板中?
问题描述
按照关于如何“编写 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>
在上面的模板中,NOTHING 在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
}
});
推荐阅读
- amazon-dynamodb - DynamoDB - GSI 创建时间如何用于按需表?
- dynamic - Teradata 中有没有办法将行转换为列而不更改对新值的查询
- javascript - 使用键盘输入登录 Angular
- angular - 我正在尝试从后端获取角色,然后在前端使用它,但 Angular 在函数后无法读取它
- javascript - 在数组数组中查找对象并返回它所属的数组
- android - 将活动名称更改为右侧?
- python - Geopandas .merge() 产生奇怪的结果
- vue.js - 如何在 node_modules 中引用静态 JS 文件?
- c# - 在 QBO 中创建估计会导致 LineDetail 类型出错
- ios - Swift 4 - 用于 Outlook 的 UIActivityViewController