Search code examples
javascriptmeteorspacebarsmeteor-helpercollectionfs

How to Upload an Image to Server on Meteor Startup


I am trying to Set a default DP for the users who do not have/not uploded an image.

Tried using helpers to display content but getting error "Uncaught Error: {{#each}} currently only accepts arrays, cursors or falsey values." Code Below(Client JS) JS :

Template.mcomments.helpers({
    'mcommentsdata':function(){
      return comdata.find({},{sort:{"at":-1}}).fetch();
    },

    images2: function () {
        var fetchimg=Meteor.users.findOne({
            "_id":this.__id
          });

        if((fetchimg.profileimage==undefined)||(fetchimg.profileimage=="")){
        var hello="/images/prof.jpg"
        return hello;
        }else{
            return Images.find({"_id":fetchimg.profileimage})


        }
    }
  });

HTML

    <template name="mcomments">
<div id="mcomments" class="col-lg-3">
    <div><h5 class="mcommentsheader">Followup Stream </h5>
    </div>
    <div class="col-lg-12 scrolling comscrolllist" id="mcomments1">
        <div id="mcomments2">
            {{#each mcommentsdata}}
            <div class="col-lg-12 comcontainer">
                <div class="row">
                    <div class="col-lg-3 indcomments" >
                        {{#each images2}}
                        <img src="{{this.url
                        }}" width="45px" height="50px" alt="">
                        {{/each}}
                    </div>
                    <div class="col-lg-9" style="">
                        <div class="row combody" >
                            <div class="pull-left mcommfont" >{{user}}</div>
                            <div class="pull-right mcommcust">{{product}} Case#{{productcaseno}}</div>
                        </div>
                        <div class="maincomment">{{comment}}</div>
                        <div class="comtemp"> <span data-livestamp="{{at}}"></span></div>
                    </div>
                </div>
            </div>

            <div class="col-lg-12 comblankspace">
            </div>
            {{/each}}
        </div>
    </div>
</div>
</template>

Now i have given up on this approach and thinking of uploading an image and attaching the url to all profiles by default(onCreateuser) until they change it.I believe to do this i might have to upload and image automatically on server startup. Please lead me to the right direction as i am still noob in meteor.

Storage Adapter: GridFS.

Regards, Azaruddin


Solution

  • Change the 'hello' variable to the following code:

    var hello = [
        { url: "/images/prof.jpg" }
    ];
    

    You're running into problems because hello is currently a string - #each expects an array/cursor/etc. as your error suggests.

    Alternatively, you could use {{#else}} on your each block and get rid of returning your 'hello' case entirely:

    {{#each images}}
        ...
    {{else}}
        <!-- Default profile image code -->
    {{/each}}