简体   繁体   中英

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?

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.

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.

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