0

I'm building a blog as a personal project, and currently I'm trying to set up RESTful routes, more or less. Specifically, I am trying to render a full Blog in a new route (after a user clicks on it in a list from the main page).

I'm stuck because when I try and Blogs.find({...}).fetch() it doesn't return a usable Blog object.

Current router.js:

// Imports and two other routes above this

FlowRouter.route('/blogs/:postId', {
    name: 'Show.Blog',
    action(params, queryParams) {
        const theId = params.postId;
        console.log(theId);
        const blog = Blogs.find({
            "_id": "tbJ9aesG99u2rRyYP",
        });
        console.log(blog)
        mount(Appshell, {
            content: <FullBlog key={blog._id} blog={blog}/>
        });
    }
})

Resulting console prints:

Object { collection: {…}, sorter: null, matcher: {…}, _selectorId: "tbJ9aesG99u2rRyYP", skip: 0, limit: undefined, fields: undefined, _projectionFn: _compileProjection(), _transform: null, reactive: true }

I tried to take that and add:

const blog = Blogs.find({
        "_id": "tbJ9aesG99u2rRyYP",
    }).collection._docs._map;

Which narrows it down to:

    {}
​        PeRTpg7FuHhLeL59o: 
            Object { _id: "PeRTpg7FuHhLeL59o", title: "On Sleep", body: "//displays the body, it's pretty long", … }
​
RPZzFov3t9L54tMT3: Object { _id: "RPZzFov3t9L54tMT3", title: "On Half-Finished Poems", body: "//displays the body, it's pretty long", … }
​
RQEtvFt3sHcspjprE: Object { _id: "RQEtvFt3sHcspjprE", title: "", body: "//displays the body, it's pretty long", … }
​
rnoZwAyb3zExdhLaL: Object { _id: "rnoZwAyb3zExdhLaL", title: "", body: "//displays the body, it's pretty long", … }
​
tbJ9aesG99u2rRyYP: Object { _id: "tbJ9aesG99u2rRyYP", title: "On and On and On", body: "//displays the body, it's pretty long", … }
​
__proto__: Object { … }

So I know it is capable of finding my data, I just can't seem to get it through the usual Docs.find({}).

What am I doing wrong?

(My repo on GitHub. The branch is ~~'aadd-mongo'~~ edit: 'add-mongo'.)

Edit: If I change it to

const blog = Blogs.findOne({
    "_id": "tbJ9aesG99u2rRyYP",
});

console.log(blog) prints undefined.

Edit: This question seems similar to what I'm experiencing. https://stackoverflow.com/a/31357970/7903932

6
  • How to add a blog post using your repo code? Commented Mar 1, 2018 at 14:58
  • @Jankapunkt sorry, I'm not sure I understand your question. Are you trying to write a blog post to my database? Commented Mar 1, 2018 at 15:10
  • Use findOne() instead of find() i.e. const blog = Blogs.findOne({ "_id": "tbJ9aesG99u2rRyYP" }); Commented Mar 1, 2018 at 15:14
  • @chridam Sorry, I tried this. It also returns undefined (both when I console.log(blog) and when I try to mount the blog). Commented Mar 1, 2018 at 15:17
  • 2
    You try to read a cursor. I think you should read again on Collections because the way you query collections looks very plain mongo-db / mongoose style and not the Meteor way. Another thing I found is that you have imported Blog but not Blogs. Commented Mar 1, 2018 at 15:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.