无法使用车把比较对象 ID?

Not able to compare Object Id using handlebars?

这是我第一次使用 Handlebars。我正在尝试将用户 _id 与登录的用户 ID 进行比较。我正在使用 mongoose 在 MongoDB 中保存数据。我从 Whosebug 找到了一个助手,但它似乎没有比较价值。用户和跟踪器的架构以及我正在使用的代码如下。

var trackerSchema = new mongoose.Schema({
    description: {
        type: String,
        required: [true, 'Please add an description']
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    user: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    }
});
mongoose.model('Tracker', trackerSchema);

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  }

});
const User = mongoose.model('User', UserSchema);

app.js 文件中的辅助函数

const hbs = exphb.create({
    extname: 'hbs',
    defaultLayout: 'mainLayout',
    layoutDir: __dirname + 'views/layouts/',
    partialsDir: __dirname + '/views/partials/',

    helpers: {
        ifEqual: function (v1, v2, options) {
            if (v1 != v2) {
                return options.inverse(this);
            }
            return options.fn(this);
        },
    }
})

在把手文件中使用辅助函数 ifEqual

{{#each list}}
<td>{{this.createdAt}}</td>
<td>{{this.user}}</td> //i am seeing this.user for example - **5ef9fb54f4bb8dff810104f7**    
<td>
    {{#ifEqual this.user looged_user_id }} //looged_user_id coming from req.user._id.toString()
    <a href="/tracker/{{this._id}}"> Edit </a>
    <a href="/tracker/delete/{{this._id}}" onclick="return confirm('Are you sure to delete this record ?');"> Delete
    </a>
    {{else}}
    <a href="#"> Edit </a>
    <a href="#" onclick="return confirm('Are you sure to delete this record ?');"> Delete
    </a>
    {{/ifEqual}}
</td>
</tr>
{{/each}}

请指导我如何比较两个 _id。

在您的代码中,您需要先将 looged_user_id 传递给模板,然后它才会进入您的辅助函数。

http://tryhandlebarsjs.com/

上试试这个

模板:

    {{#each list}}
    <td>{{this.createdAt}}</td>
    <td>{{this.user}}</td> 
    <td>
        {{#ifCond this.user}} //Here 1 is just hardcoded insted of that need to change with actal variable
        <a href="/tracker/{{this._id}}"> Edit for if </a>
        <a href="/tracker/delete/{{this._id}}" onclick="return confirm('Are you sure to delete this record ?');"> Delete looged_user_id
        </a>
        {{else}}
        <a href="#"> Edit for else </a>
        <a href="#" onclick="return confirm('Are you sure to delete this record ?');"> Delete
        </a>
        {{/ifCond}}
    </td>
    </tr>
    {{/each}}

上下文:

    {
        "list": [{
            "user": 1,
            "content": "hello"
        },{
            "user": 2,
            "content": "world"
        }],
    }

注册辅助函数:

    Handlebars.registerHelper('ifCond', function(v1, options) {
      if(v1 === looged_user_id ) {
        return options.fn(this);
      }
      return options.inverse(this);
    });

结果:

    <td></td>
    <td>1</td> 
    <td>
        <a href="/tracker/"> Edit for if </a>
        <a href="/tracker/delete/" onclick="return confirm('Are you sure to delete this record ?');"> Delete looged_user_id
        </a>
    </td>
    </tr>
    <td></td>
    <td>2</td> 
    <td>
        <a href="#"> Edit for else </a>
        <a href="#" onclick="return confirm('Are you sure to delete this record ?');"> Delete
        </a>
    </td>
    </tr>

您遗漏了两件事:

  1. 正在发送 looged_user_id 到您的 hbs:

    res.render("tracker/list", { list: body, logged_user_id: req.user._id, });

  2. 在 #each handlebar 循环中以正确的方式访问 logged_user_id 它。在循环内部时,您不能直接使用传递的数据。您必须提供 logged_user_id 的正确路径,在您的情况下为 ../logged_user_id.

../ 路径段引用父模板范围,您可以使用它访问 logged_user_id。