从bodyParser获取未识别的内容

huangapple go评论71阅读模式
英文:

Getting unidentified from bodyParser

问题

我正在开始学习Node.js + Express + Handlebars,但在处理我的POST请求时遇到了困难。这是我接收到的请求体,我似乎无法通过req.body.provincereq.body.municipality来获取“省份和市镇”数据,但对于bearing,我可以。

{
  'province ': 'ALBAY',
  'municipality ': 'BACACAY',
  bearing: '>=1 and <=20'
}

这是我从表单中获取市镇和省份数据的方式。

<div class="form-group">
  <label for='municipality'>Municipality</label>
  <select type="text" name="municipality " class="form-control">
    {{#each municipality}}
    <option>{{municipality}}</option>
    {{/each}}
  </select>
</div>

以及用于bearing的部分。

<div class="form-group">
  <label for="bearing">Bearing Trees</label>
  <select type="text" name="bearing" class="form-control">
    <option>>=1 and <=20</option>
    <option>>=21 and <=50</option>
    <option>>=51 and <=100</option>
    <option>>100</option>
  </select>
</div>

我现在有点卡住,需要您的帮助。谢谢!

英文:

I'm just starting learning nodejs + express + handlebars and I'm having a difficult time with my post. This is the request body that I am receiving and I can't seem to get the "province and municipality" data using req.body.province and req.body.municipality, but with bearing, I can.

{
&#39;province &#39;: &#39;ALBAY&#39;,
  &#39;municipality &#39;: &#39;BACACAY&#39;,
  bearing: &#39;&gt;=1 and &lt;=20&#39;
}

This is how I get my data from my form for municipality and province.

&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&#39;municipality&#39;&gt;Municipality&lt;/label&gt; &lt;select type= &quot;text&quot; name=&quot;municipality &quot; class=&quot;form-control&quot;&gt; {{#each municipality}}
 &lt;option&gt;{{municipality}}&lt;/option&gt; 
{{/each}} &lt;/select&gt;

And this is for bearing.

&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;bearing&quot;&gt;Bearing Trees&lt;/label&gt; &lt;select type=&quot;text&quot; name=&quot;bearing&quot; class=&quot;form-control&quot;&gt;
 &lt;option&gt;&gt;=1 and &lt;=20&lt;/option&gt;
&lt;option&gt;&gt;=21 and &lt;=50&lt;/option&gt;
&lt;option&gt;&gt;=51 and &lt;=100&lt;/option&gt; &lt;option&gt;&gt;100&lt;/option&gt; &lt;/select&gt; &lt;/div&gt;

I'm kinda stuck now and I need your help. thanks

答案1

得分: 0

从我所看到的,与市和省对应的键末尾包含一个空格。

在对象内,所有的键都是字符串类型,这意味着空格是包含在内的,除非你使用一个符号。

对于你的问题,更多是拼写错误而不是其他什么。

你的对象应该像这样:

{
    province: 'ALBAY',
    municipality: 'BACACAY',
    bearing: '>=1 and <=20'
}
英文:

From what i can see, the keys corresponding to municipality and province contains a space at the end.

Inside an object all the keys are of type string, which means the space is included unless you are using a symbole.

For your question it's more a typo that anything else.

your object should looks like this :

{
    province: &#39;ALBAY&#39;,
    municipality: &#39;BACACAY&#39;,
    bearing: &#39;&gt;=1 and &lt;=20&#39;
}

huangapple
  • 本文由 发表于 2020年1月6日 22:35:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定