简体   繁体   English

流星Session.get()返回正确的值,但带有额外的空格/行

[英]Meteor Session.get() returning correct value but with extra whitespace / lines

I can't figure out why Session.get() is returning the expected property, but with some strange formatting. 我不知道为什么Session.get()返回期望的属性,但是格式有些奇怪。

In the console: 在控制台中:

>Session.get('department');

returns (eg): 返回(例如):

"
    7
  "

My objects don't seem to have any strange formatting in their properties. 我的对象的属性似乎没有任何奇怪的格式。 For example: 例如:

>Suppliers.findOne();

Returns 返回

Object {_id: LocalCollection._ObjectID, 
        vndrLongNm: "*ERSACE", 
        venOpCode: 7, 
        supSiteNum: 7839748, 
        vndrTypeCd: "B"…}
        _id: LocalCollection._ObjectID_str: "547f4cfe2979008172bb7071"__proto__: LocalCollection._ObjectID
        deptDesc: "980-DIRECT GIRLS 7-14"
        supSiteNum: 7839748
        venDptNum: 980
        venOpCode: 7
        venResNum: 58794
        vndrLongNm: "*ERSACE"
        vndrTypeCd: "B"
        __proto__: Object}

Which looks right to me. 在我看来,这很对。 Note that the value for venDptNum is a number – 980 – with no apparent extra spacing or extra blank lines. 请注意, venDptNum的值是一个数字– 980 –没有明显的额外空格或额外的空行。

The following mongo query returns a sorted list of unique venDptNum s: 以下mongo查询返回唯一的venDptNum的排序列表:

Template.departments.helpers({
  departmentNames: function() {
    var everything = Suppliers.find({}, {sort: {venDptNum:1}}).fetch();
    var justDepartments = _.pluck(everything,"venDptNum");
    return _.uniq(justDepartments);
  }

It does return the list and populates a dropdown with the list of venDptNum properties, as intended. 确实会返回该列表,并按预期使用venDptNum属性列表填充一个下拉列表。 I then take that selection and set it as the session variable value for department : 然后,我选择该选项并将其设置为department的会话变量值:

Template.departments.events({
  "change .department-selection": function(e, t){
    return Session.set("department", $("[name=departmentNames]").val());
  }
});

I have almost the exact same code running here . 在这里运行几乎完全相同的代码。 If you're curious, try running Session.get('department'); 如果您好奇,请尝试运行Session.get('department'); and see that it's returning the value as expected. 并看到它正在返回预期的值。

[Note: this was at least part of the problem I discovered while answering this question.] [注意:这至少是我在回答问题时发现的问题的一部分。]

It was an error in the template: 模板中存在错误:

<template name="vendorNames">
  <select name="vendorNames" autocomplete="off" class="form-control vendor-name-selection">
    {{# each vendorNames}}
    {{> vendorName}}
    {{/each}}
  </select>
</template>

<template name="vendorName">
  <option name="vendor">
    {{vndrLongNm}}
  </option>
</template>

The vendorName template was forcing a new line and adding that (apparently) into the value. vendorName模板强制使用新行并将其(显然)添加到值中。 Solution was changing the formatting to: 解决方案是将格式更改为:

<template name="vendorNames">
  <select name="vendorNames" autocomplete="off" class="form-control vendor-name-selection">
    {{# each vendorNames}}
    {{> vendorName}}
    {{/each}}
  </select>
</template>

<template name="vendorName">
  <option name="vendor">{{vndrLongNm}}</option>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM