Search code examples
javascriptnode.jsdatabasemongodbhandlebars.js

Handlebars eq function with other data models


 {{#times league.groupNumber}}
                    <form action="/ayarlar/league/edit-{{this}}/{{../league._id}}?_method=PUT" method="POST"
                        class="boxInfo">
                        <div class="grupDiv"
                            style="width:150px;display:inline-block;border:1px solid black;padding:10px;">
                            <h2>Grup {{this}}</h2>
                            <select name="groupNo{{this}}" id="" multiple>
                                {{#each ../team}}
                                <option value="{{_id}}">{{#if (eq _id ../../league.groupNo1.[0]._id ) }}Warning! {{/if}}{{teamName}}</option>
                                {{/each}}
                            </select>
                            <input type="hidden" name="_method" value="PUT">
                            <button type="submit" class="greenButton">Ekle</button>
                            <ul>
                                <span style="font-weight:600;">Takımlar : </span>
                                {{#each (lookup ../league (concat 'groupNo' this))}}
                                <li>{{this.teamName}}</li>
                                {{/each}}
                            </ul>
                        </div>
                    </form>
{{/times}}

This is my code block and here is code which i had problem.

{{#each ../team}}
   <option value="{{_id}}">{{#if (eq _id ../../league.groupNo1.[0]._id ) }}Warning! {{/if}}{{teamName}}</option>
{{/each}}

When I run this code if the current _id is equal to league.groupNo1 elements _id , I want to see warning text before teamName. But I cannot see anything. Also When write code like this, I can see values and they are same.

{{#each ../team}}
     <option value="{{_id}}">{{_id}}</option>
     <option value="">{{../../league.groupNo1.[0]._id}}</option>
     <option value="{{_id}}">{{#if (eq _id ../../league.groupNo1.[0]._id ) }}Uyarı{{/if}}{{teamName}}</option>
{{/each}}

And also here is my schema's:

const LeagueSchema = new mongoose.Schema({
    leagueName : {type:String, require:true, unique: true},
    typeSelect : {type:String, require: true},
    importanceOfLeague : {type: Number, require:true},
    leagueMiddleRefereeIsChecked : {type:Boolean, default:false},
    leagueLinemanIsChecked : {type:Boolean, default:false},
    leagueSecondLinemanIsChecked: {type:Boolean, default:false},
    leagueFourthRefereeIsChecked : {type:Boolean, default:false},
    leagueVarRefereeIsChecked : {type:Boolean, default:false},
    leagueSecondVarRefereeIsChecked: {type:Boolean, default:false},
    groupNumber: {type:Number},
    groupNo1: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo2: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo3: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo4: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo5: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo6: [{ type: Schema.Types.ObjectId, ref: "team"}],
    groupNo7: [{ type: Schema.Types.ObjectId, ref: "team"}],
const TeamSchema = new mongoose.Schema({
    teamName: { type: String, require:true},
    leagueName: { type: Schema.Types.ObjectId, ref: "league"},
    date: { type: Date, default: Date.now },
    isTeamSelected: {type:Boolean, default:false},
})

And here is my routher.get code:


router.get("/:id",(req,res) =>{
    Team.find({ leagueName: req.params.id }).sort({teamName:1}).lean().then(team =>{
        League.findById(req.params.id)
        .populate(teamPaths)
        .then(league => {
            Team.find({ leagueName: req.params.id }).sort({teamName:1}).lean().then(team =>{
                res.render("site/leagueDataScreen", {league:league.toJSON(),team:team})
    
            })
        })
    })
    

})

I tried use lookup function but I got errors.

{{#each ../team}}
  <option value="{{_id}}">{{#if (eq _id (lookup ../../league.groupNo1.[0]._id)) }}Warning! {{/if}}{{teamName}} </option>
{{/each}}

Solution

  • The comparison fails because you are comparing objects, not strings; and objects are equal when they have the same reference, which yours do not.

    Leveraging this post about comparing Mongoose Object IDs (which I would recommend you read and up-vote if it is helpful to you), I would suggest creating a new Handlebars Helper to compare Object IDs. For example,

    eqIds: function (id1, id2) {
      return id1.equals(id2);
    }