奇怪的问题:前端handlebars 从数组里取值object的 key时出现问题,不可能的代码取出了值?

前端代码

    {{#each exchangeBests}}
    <article>
      <div class="columns is-half-desktop article-first">
        <div class="column">
          <img src="https://images.t.com/f_auto,q_auto/v1/production/foundation-entities/uploads/photo/dating.20210105130957.jpg" alt="">
        </div>
        <div class="column">
  
          <h4>{{this.title}}</h4>
          <p> <span class="numRadius">5</span>  {{this.updated_at}}</p>

        </div>
      </div>
    </article> 
    {{/each}}

后端代码

let exchangeBests = await Tag.findOne({name:'Exchange'},{title:1, updated_at:1, bests:1}).populate('bests');
let walletBests = await Tag.findOne({name:'Wallet'},{title:1, updated_at:1,bests:1}).populate('bests');

logger.debug('exchangeBests: '+ JSON.stringify(exchangeBests.bests));
logger.debug('walletBests: '+ JSON.stringify(walletBests.bests));

res.render('home/home', {
    exchangeBests: exchangeBests.bests,
    walletBests: walletBests.bests,
}); //render 



logger.debug 结果

2022-10-07T09:31:19.851Z [right meow!] debug: exchangeBests: 

[{"_id":"633c1f2ecf58007e7b0e930d", "title":"6778 crypto  in 2","created_at":"2022-10-04T11:55:26.493Z","updated_at":"2022-10-06T07:32:25.858Z"}]


2022-10-07T09:31:19.851Z [right meow!] debug: walletBests:

 [{"_id":"633c1d746bbdb2b65407aee0","tags":["633c1d746bbdb2b65407aee0","633c1d746bbdb2b65407aee0"],"title":"haha11 crypto  in 2","created_at":"2022-10-04T11:48:04.967Z","updated_at":"2022-10-04T11:48:05.339Z"}]

看上面代码,可知exchangeBests和walletBests都是正确的,

问题出来了,我用{{#each }} {{this.title}} {{/each}}, 确取不出title问下有没有大神知道是什么原因呢?

备注:

奇怪得是,我这样竟然能出来数据:

    {{#each exchangeBests}}
    <article>
        </div>
        <div class="column">

         {{#each this}}
          <h4>{{title}}</h4>
          <p> <span class="numRadius">5</span>  {{updated_at}}</p>
          {{/each}}


    </article> 
    {{/each}}

阅读 1.4k
1 个回答

很久很久不用 handlebars,无法给出准确答案。

不过我印象里不需要 this,直接写 {{title}} 就好。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏