You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Handlebars无法渲染用户名称,登录后else分支正常

问题:Handlebars无法渲染{{user.name}}但else分支正常触发

登录后页面能正常触发Handlebars的else分支,但{{user.name}}始终无法显示,已重置数据库并重新创建用户,问题依旧。

Handlebars模板代码

<div class="profile">
    <div class="profile-header">
       {{user.name}}
    {{#if portfolio.length}}
        {{#each portfolio as |portfolio|}}
            <div class="profile-port">
                {{> port-info portfolio}}
                <button type="submit" class="btn profile-btn"><a href="/profile/edit/{{portfolio.id}}"> Edit Info</a></button>
                <button type="submit" class="btn profile-btn">View Profile</button>
            </div>
        {{/each}}

        {{else}}
        <h4>No user information added.</h4>
        <button type="submit" class="btn profile-btn"><a href="/profile/create"> Create Portfolio</a></button>

    {{/if}}
    </div>
</div>

Sequelize User模型代码

const { Model, DataTypes } = require('sequelize');
const bcrypt = require('bcrypt');
const sequelize = require('../config/connection');

class User extends Model {
  checkPassword(loginPw) {
    return bcrypt.compareSync(loginPw, this.password);
  }
}

User.init(
    {
      id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
      },
      name: {
        type: DataTypes.STRING,
      },
      github: {
        type: DataTypes.STRING,
        allowNull: true
      },
      email: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true,
        validate: {
          isEmail: true,
        },
      },
      password: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          len: [8],
        },
      },
    },
    {
      hooks: {
        beforeCreate: async (newUserData) => {
          newUserData.password = await bcrypt.hash(newUserData.password, 10);
          return newUserData;
        },
        beforeUpdate: async (updatedUserData) => {
          updatedUserData.password = await bcrypt.hash(updatedUserData.password, 10);
          return updatedUserData;
        },
      },
      sequelize,
      timestamps: false,
      freezeTableName: true,
      underscored: true,
      modelName: 'user',
    }
  );
  
  module.exports = User;

排查与解决方向

  • 检查模板渲染上下文:确认路由/控制器中传递给res.render的对象包含user键,且该对象的name字段有值。比如Express中需确保代码类似res.render('profile', { user: req.session.user, portfolio: ... })。
  • 验证数据库数据:直接查询user表,确认目标用户的name列不为空值,排除创建用户时未填入name的情况。
  • 检查Sequelize查询:如果通过User.findOne等方法获取用户,确保未通过attributes选项排除name字段,默认会返回所有字段,但手动指定时需包含name。
  • 确认会话数据完整性:登录时需将用户的name字段存入session,比如验证通过后执行req.session.user = { id: user.id, name: user.name, email: user.email },避免只存储id。
  • 调试模板上下文:在模板中临时添加{{log user}},查看控制台输出的user对象结构,确认name属性是否存在,或键名是否有误(比如是否是username而非name)。

内容的提问来源于stack exchange,提问作者Bryce Bann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 10:20:39