javascript - 查询 Meteor 集合以获取 afQuickField select 的值失败。为什么?

标签 javascript mongodb meteor meteor-autoform

我的代码中是否犯了错误,或者我是否在 autoform 或 Meteor 中发现了错误?我坚信这是我的错误,但找不到它。

在我的 Meteor 应用程序中,当查询集合以为表单中的选择输入字段提供值时,在服务器上运行的函数返回正确填充的数组,但在客户端上运行时返回空数组。

该应用程序将跟踪医学研究课题。我有研究和研究赞助商的收藏。创建或编辑研究时,我想从现有赞助商列表中输入研究赞助商。 Autoform 和 simple-schema 用于创建创建和编辑表单。研究发起人名称的选择输入由研究架构中的名称作为给定“allowedValues”的数组提供。当给 allowedValues 一个显式数组时,一切都会按预期工作。但是,当数组由函数提供时

sponsorNames = function(){
  sn =  Sponsors.find().map(function(sp){ return sp.sponsorname });
  console.log(sn);
  return sn;
};

从 Sponsors 集合中收集数组值,生成的列表为空。

sponsorNames 函数中的 console.log 语句将填充的数组打印到我运行应用程序的 cli。但是,浏览器控制台显示来自同一 console.log 语句的空数组。我相信代码在服务器和客户端上运行都会产生两个不同的结果。在所有其他方面,代码都按其应有的方式运行。

简化的应用程序结构:

research
  both
    collections
      sponsors.js
      studies.js
    router
      routes.js
  client
    sponsor
      events.js
      helpers.js
      templates.html
    study
      events.js
      helpers.js
      templates.html
      subscribe.js
  server
    methods.js
    publish.js
    security.js

两者/collections/sponsors.js

Sponsors = new Meteor.Collection('sponsors');

Schema.Sponsors =  new SimpleSchema({
  sponsorname: {
    type: String,
    label: 'Sponsor Name'
  },
});

Sponsors.attachSchema(Schema.Sponsors);

两者/collections/studies.js

Studies = new Meteor.Collection('studies');

sponsorNames = function(){
  sn =  Sponsors.find().map(function(sp){ return sp.sponsorname });
  console.log(sn);
  return sn;
};

Schema.Studies =  new SimpleSchema({
  studyname: {
    type: String,
    label: 'Study Name'
  },
  sponsor: {
    type: String,
    label: 'Sponsor',
    allowedValues: sponsorNames(),
  },
  sitenum: {
    type: String,
    label: 'Site Number'
  },
});

Studies.attachSchema(Schema.Studies);

client/study/templates.js

<template name='editStudy'>
  {{#autoForm collection=studies id="updateStudyForm" type="update" doc=doc}}
    <fieldset>
      <legend>Edit a Study</legend>
      {{> studyPanel1}}
    </fieldset>
    <button type="submit" class="btn btn-primary">Update</button>
  {{/autoForm}}
</template>

<template name='studyPanel1'>
      {{> afQuickField name="studyname" class="form-control input"}}
      {{> afQuickField name="sponsor" class="form-control input" options='allowed' }}
      {{> afQuickField name="sitenum" class="form-control input"}}
</template>

client/study/helpers.js

Template.addStudy.helpers({
  studies: function(){
    return Studies;
  },
});

Template.editStudy.helpers({
  studies: function(){
    return Studies;
  },
  doc: function(){
    return this;
  }
});

client/study/events.js

var sponsorHooksObject = {
  after: {
    insert: function(error, result) {
      if (!error) {
        Router.go('sponsorsPage');
      };
    },
    update: function(error, result) {
      if (!error) {
        Router.go('sponsorsPage');
      };
    }
  },
};

AutoForm.hooks({
  insertSponsorForm: sponsorHooksObject,
  updateSponsorForm: sponsorHooksObject
});

客户端/subscribe.js

Meteor.subscribe('Subjects');
Meteor.subscribe('Studies');
Meteor.subscribe('Sponsors');

服务器/methods.js

Meteor.methods({
  'removeSubjectData': function(id){
    Subjects.remove(id);
  },

  'removeStudyData': function(id){
    Studies.remove(id);
  },

  'removeSponsorData': function(id){
    Sponsors.remove(id);
  },
});

服务器/publish.js

Meteor.publish('Subjects', function(){
  return Subjects.find({});
});

Meteor.publish('Studies', function(){
  return Studies.find({});
});

Meteor.publish('Sponsors', function(){
  return Sponsors.find({});
});

服务器/security.js

Subjects.permit(['insert', 'update', 'remove']).apply();

Studies.permit(['insert', 'update', 'remove']).apply();

Sponsors.permit(['insert', 'update', 'remove']).apply();

meteor 列表:

accounts-password            1.1.1
alanning:roles               1.2.13
aldeed:autoform              5.3.2
aldeed:collection2           2.3.3
aldeed:simple-schema         1.3.3
aslagle:reactive-table       0.8.9
email                        1.0.6
fortawesome:fontawesome      4.3.0
ian:accounts-ui-bootstrap-3  1.2.71
iron:router                  1.0.9
meteor-platform              1.2.2
ongoworks:security           1.2.0
reactive-var                 1.0.5
twbs:bootstrap               3.3.5

最佳答案

我只是想错了。用于填充选择字段的 Sponsors 集合的查询属于模板帮助程序。通过这些更改,应用程序可以正常工作。

两者/collections/studies.js

Studies = new Meteor.Collection('studies');

// Remove these:
// sponsorNames = function(){
//   sn =  Sponsors.find().map(function(sp){ return sp.sponsorname });
//   console.log(sn);
//   return sn;
// };

Schema.Studies =  new SimpleSchema({
  studyname: {
    type: String,
    label: 'Study Name'
  },
  sponsor: {
    type: String,
    label: 'Sponsor',
    // Remove this:
    // allowedValues: sponsorNames(),
  },
  sitenum: {
    type: String,
    label: 'Site Number'
  },
});

Studies.attachSchema(Schema.Studies);

client/study/templates.js

<template name='editStudy'>
  {{#autoForm collection=studies id="updateStudyForm" type="update" doc=doc}}
    <fieldset>
      <legend>Edit a Study</legend>
      {{> studyPanel1}}
    </fieldset>
    <button type="submit" class="btn btn-primary">Update</button>
  {{/autoForm}}
</template>

<template name='studyPanel1'>
      {{> afQuickField name="studyname" class="form-control input"}}
      {{> afQuickField name="sponsor" class="form-control input"
                       type='select'  options=sponsorNames }}  <!-- Add this: -->
      {{> afQuickField name="sitenum" class="form-control input"}}
</template>

client/study/helpers.js

Template.addStudy.helpers({
  studies: function(){
    return Studies;
  },
});

Template.editStudy.helpers({
  studies: function(){
    return Studies;
  },
  doc: function(){
    return this;
  }
});

// Add this:
Template.registerHelper("sponsorNames", function() {
  return Sponsors.find().map(function(sp){
    return {label: sp.sponsorname, value: sp.sponsorname};
  });
});

关于javascript - 查询 Meteor 集合以获取 afQuickField select 的值失败。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31384688/

相关文章:

javascript - 空对象 meteor / react 的后备消息

javascript - meteor .js 1.0 : what is the difference between the packages and versions file?

javascript - 如何加载比加载 html 文件更高的一个文件夹中可用的脚本文件

javascript - 如何在javascript中获取startdatetime和enddatetime之间的差异

javascript - 为什么只有一个 Node readline 对象可以工作?

arrays - 如何在mongodb中连接数字和字符串值?

javascript - 如何防止从 JavaScript 控制台调用 'Meteor.call'?

javascript - 如何使用另一个 .js 文件中的函数?

node.js - Mongoose - 使用 .populate 访问嵌套对象

Ansible playbook 中的 Mongodb 查找错误