亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在服務器端將隱藏值數據作為數組發送時出現錯誤

在服務器端將隱藏值數據作為數組發送時出現錯誤

呼啦一陣風 2021-03-29 21:59:10
我正在使用把手作為模板引擎,并且是把手的新手。我從API(edamam配方搜索api)傳遞了數據。當嘗試使用表格中的隱藏值將附在每個配方卡上的配料陣列發回時,在服務器端出現錯誤??刂婆_顯示[對象對象],[對象對象],[對象對象],[對象對象],[對象對象],[對象對象] [對象對象],[對象對象],[對象對象],[對象對象],[對象對象],[對象對象]嘗試在服務器端注銷時。不知道發生了什么。下面的代碼:<div class="container">  <header class="jumbotron">    <div class=container></div>    <h1>{{currentUser.username}}</h1>    <h1>Press save to add the recipes to your dashboard</h1>    <p>      <a class="btn btn-primary btn-large" href="/{{currentUser.username}}/recipes/dashboard">Go To Your Dashboard</a>    </p>  </header>  <div class="row text-center" style="display:flex; flex-wrap:wrap">    {{#each data}}    <div class="col-md-3 col-sm-6">      <div class="thumbnail">        <img src="{{recipe.image}}" alt="Recipe Pic">        <div class="caption">          <h4>            {{recipe.label}}          </h4>          <h5>            Ingredients          </h5>{{!-- recipe.ingredients is an array of ingredient objects with text as a key --}}          {{#each recipe.ingredients}}           <p>{{text}}</p>          {{/each}}        </div>        <p>         <form id="buttonDesign" action="/recipes/dashboard" method="POST">         <input type="hidden" name="recName" value="{{this.recipe.label}}"/>         <input type="hidden" name="recImage" value="{{this.recipe.image}}"/>         <input type="hidden" name="recUrl" value="{{this.recipe.url}}"/>         <input type="hidden" name="recIngredients" value "{{this.recipe.ingredients}}"/>            <button class="btn btn-primary">Save</button>          </form>        </p>      </div>    </div>   {{/each}}  </div></div></div>如上所述,當我在服務器端注銷req.body.recIngredients時,[object, Object]出現錯誤。
查看完整描述

2 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

使用模板引擎傳遞的數據是直接對象,例如{{this.recipe.ingredients}},因此當此對象“ this.recipe.ingredients”轉換為字符串時,它將轉換為“ [[Object object]]”是Object#toString()方法提供的默認字符串響應。您需要做的是首先將您的對象轉換為字符串,然后將其分配給html元素的屬性值。為了進行轉換,您可以使用“ JSON.stringify(this.recipe.ingredients)”,它將整個對象轉換為JSON格式的字符串。我不知道“ Handlebars模板引擎”,但這應該可以工作:{{JSON.stringify(this.recipe.ingredients)}}。是的,您忘記了放置“ =”<input type="hidden" name="recIngredients" value "{{this.recipe.ingredients}}"/>來將值屬性與其實際值(即“ {{this.recipe。

查看完整回答
反對 回復 2021-04-08
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

實際上說您的代碼按預期工作。在將JAVASCRIPT對象(this.recipe.ingredients)放在隱藏字段中時,我們需要將該JAVASCRIPT對象轉換為字符串值,以便將其作為FORM數據提交。


要進行轉換,您需要創建并注冊如下所示的把手助手


Handlebars.registerHelper('json', function(context) {

    return JSON.stringify(context);

});

另外,您需要在如下所示的適當位置使用該幫助程序。


<input type="hidden" name="recIngredients" value="{{json this.recipe.ingredients}}"/>

順便說一句,如果將隱藏字段設置為文本字段,則可以輕松找到問題(我希望)。


查看完整回答
反對 回復 2021-04-08
  • 2 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號