Problem with insert operations that depend on each other and client/server syncing

43 views
Skip to first unread message

jim...@ghostcrabworkshop.com

unread,
Feb 25, 2013, 4:20:20 PM2/25/13
to meteo...@googlegroups.com
I have a couple of related Meteor collections – Boards and Items. Items belong to Boards, so they have fields named board, which are references to the _ids of board records in Mongo.

I'm trying to implement a copyBoard method, which: 

1. Inserts a copy of a board record
2. Inserts copies of all of the items associated with that board and sets their board fields to the new board record created in step 1.

It looks like this currently: https://gist.github.com/jimkang/5033319

The problem I'm running into is that the client side stub gives me a new board record that has a temporary _id. I use that to set the board field of the new item records created in duplicateRecords. When Meteor syncs it back to the server, it changes that board's _id, but the new item records are stuck with the client-side temporary _id in their board fields, which now points to nothing.

I was thinking of not inserting the items until the new board has synced to the server, but I'm not sure how to do that without publishing a separate "new board" collection. Seems there's probably a better approach. Is there?

Dror Matalon

unread,
Feb 25, 2013, 4:40:57 PM2/25/13
to meteo...@googlegroups.com

I would do this on the server. Why fetch all the data into the client and then send it back? Just create a method copyBoard(id) on the server that does all of this. The code should be quite similar.

--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

jim...@ghostcrabworkshop.com

unread,
Feb 25, 2013, 6:47:05 PM2/25/13
to meteo...@googlegroups.com
Hey, thanks! OK, two basic things I did not understand:

1. You can use Meteor.methods to make server-only methods that can be called from the client. I thought the point of it was that it was for making methods that have both a server and client stub version. It turns out I need to read more carefully!

2. Mongo bulk insert isn't supported. Calls to insert with an array as the parameter will return an id, but when you try to look that up later, you get nothing.

Now, I have copyBoard working.

David Glasser

unread,
Feb 26, 2013, 1:47:34 PM2/26/13
to meteo...@googlegroups.com
Yeah, this is annoying now. Dealing with this is on the Meteor pre-1.0 roadmap: https://trello.com/card/pattern-for-creating-multiple-database-records-from-a-method/508721606e02bb9d570016ae/57

As of 0.5.5 you can specify your own _id's, so you could generate some ids in client code (with Random.id(), eg) and pass them as arguments to the copyBoard method. That way the same IDs can be used on the server and in the stub. It's definitely kind of annoying, though.


On Mon, Feb 25, 2013 at 1:20 PM, <jim...@ghostcrabworkshop.com> wrote:

--

jim...@ghostcrabworkshop.com

unread,
Feb 26, 2013, 10:06:01 PM2/26/13
to meteo...@googlegroups.com
Ah, it'll be great if it handles that, as I can see needing data hierarchies with more than two levels someday.

Fortunately, not for this app, though, so passing an id for each item from the client will do. Thanks!
Reply all
Reply to author
Forward
0 new messages