Passing data between links using iron router


#1

I’ll try to explain my issue properly and easy to understand.
I am creating a messaging app similar to whatsapp any of those others

I have a form that when submitted successfuly creates a new chat thread and a link for that thread
templates > chatList.html (snippet)

{{#each chatList}}
   <li><a href="{{pathFor route='messagesList'}}">{{name}}: {{lastMessage}}</a></li>
{{/each}}

I have a pages that list all my chat thread (ChatList collection) from different people. And those each link to a page of the messages that Chat contains (MessagesList collection. The messages list is populated by matching chatID field (attached to each message) in my MessagesList collection with the _id field (attached to each chat thread) in my ChatList collection.
However, i am not able to access the chat _id field when viewing a messages list. I can only access it when i am viewing the chat list.
For testing, i have a console.log statement that prints the _id of the chat when viewing the from the chats list and when viewin from the messages list. It is undefined when viewing on the messages list page but accessible on the chats list page

How do i pass the the _id field from chat lists into the messages list to be accessible? It seems that since i am using iron router to create the links, it is causing problems passing data.

let me know if that made any sense or if you have questions


#2

Check out iron router, you need to pass the _id via the data parameter of pathFor

{{#each cl as chatList}}
   <li><a href="{{pathFor route='messagesList' data=cl}}">{{cl.name}}: {{cl.lastMessage}}</a></li>
{{/each}}

#3

I guess I am a bit confused or maybe i wasnt specific enough. Tough to explain.
I have a page where a user creates a new chat thread using 2 input boxes where they enter a users email and a message. Submitting that form creates the chat thread and brings you to a route where you being typing messages to the user email you enter on the previous form. I need to pass that inputed email into the messages pages.

localhost:3000/chatlist --> input email of user you want to send message to and initial message.

…chat thread created

localhost:3000/chatlist/4jf4jsljf924mfdalkoq --> begin sending messages to email of user entered on last page.

-> How can i access the email enter on the /chatlist page on the chatlist/4jf4jsljf924mfdalkoq page?


#4

Maybe you are a little confused because you think about submit a form in the ‘classic’ way like you would do in PHP for example. In Meteor it’s not exactly the same thing and the events are extremely important. Plus, I don’t know exactly how you collections are as well as the routes, but let’s see if I can help you in the some way.

Let’s say you probably have a collection of users (with _id, email address, etc…). Why don’t you bind the chat session to the user for example? That would help you a lot.
So, you would have this route localhost:3000/chat/user_id that is exactly the chat with that user with that email address. Once you are there, you can retrieve all the data about that user, having the user id available (you can get it via the route params).

About the form…when you submit the form, capture that event with ‘submit #form_id’…and there do something like:
var email = $('#email_input').val(); var user = UserCollection.findOne({'email': email}); Route.go('chat.route', {_id: user._id});

Take also a better look at the Iron documentation, so you can better understand how flexible it is and figure out the best practice.