Jamgreen
Jamgreen

Reputation: 11059

Create document after submit with meteor-autoform

I am using meteor-autoform. I create my form with

{{> quickForm collection="Messages" id="insertMessageForm" type="insert" fields="text"}}

It inserts the messages as it should but I also want to create a document in the Notification collection. How can I make sure an notification is created every time a new message is created? I want to create notification each time a new document is created in a collection all over my app. How can this be done smartest? Can I create an afterCreate signal or something?

Upvotes: 2

Views: 135

Answers (2)

halbgut
halbgut

Reputation: 2386

Use the meteor-core feature cursor.obsere

lib/

Messages.observe({
  added: function (doc) {
    Notifications.insert({ text: 'New Message: ' + doc.text })
  }
})

The doc variable holds the new document that was inserted.

Upvotes: 0

saimeunt
saimeunt

Reputation: 22696

I want to create notification each time a new document is created in a collection all over my app.

Then you should probably use this package : matb33:collection-hooks

You will be able to create hooks for each of your collections to create a notification when a new document is inserted.

Comments.after.insert(function(userId, comment){
  Notifications.insert({
    userId: userId,
    text: comment.text,
    createdAt: comment.createdAt
  });
});

Be careful when using this package to not overly complicate your application logic and create circular hooks.

Upvotes: -1

Related Questions