简体   繁体   中英

Meteor render data from embeded, nested collection

I've been trying to get data out of a nested collection without any luck, besides using the dot notation from the html nothing seems to work.

What I want basically is to soley fetch the data I need from a nested collection. I'm trying to build a file upload for images and audio files and then a simple way to use the files. I'm using the cfs:standard-packages and cfs:filesystem packages.

The code below shows a working example of what I don't want, eg fetching the whole file object and retrieving the data in the html. If I could use the dot notation in the mongo command somehow would be perfect. I also could settle for _each but I would prefer fetching just the data I need on each db call. As you can see I'm passing an id for the whole file object here. Uploads.find({_id:Session.get('getpic')}); BTW, the actual file is stored in a folder on my local server.

The collection:

 {
    "_id" : "DXFkudDGCdvLpPALP",
    "original" : {
        "name" : "drink.png",
        "updatedAt" : ISODate("2015-04-30T07:14:56.000Z"),
        "size" : 127944,
        "type" : "image/png"
    },
    "uploadedAt" : ISODate("2015-07-11T21:53:32.526Z"),
    "copies" : {
        "uploads" : {
            "name" : "drink.png",
            "type" : "image/png",
            "size" : 127944,
            "key" : "uploads-DXFkudDGCdvLpPALP-drink.png",
            "updatedAt" : ISODate("2015-07-11T21:53:32.000Z"),
            "createdAt" : ISODate("2015-07-11T21:53:32.000Z")
        }
    }
}

HTML

<template name="renderImages">
{{#each showpic}}
    <img width="300" height="300" src="/projectuploads/{{copies.uploads.key}}"/>
{{/each}}

Javascript:

Template.renderImages.helpers({
    showpic: function() {
    return Uploads.find({_id:Session.get('getpic')});
  }
});

specify the returned fields in the find query like so

return Uploads.find({_id:Session.get('getpic')}, { fields: {'copies.uploads.key': 1} } );

but a word on that. here you query minimongo (on the client), which is in the browsercache so it's basically free. take care to publish only those fields to the client that you actually want there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM