流星:如何检索方法中插入的_id



我有一个 meteor 方法来插入文档。该文档的 ID 用作 url 扩展名。创建文档后,我还通过相同的方法发送电子邮件。在电子邮件中,我想包含一个指向网址扩展名的链接。我该怎么做?

//create a video
Meteor.methods({
  createVideo: function(doc) {
    Videos.insert({title: doc.title, userId: this.userId, date: new Date()});
    var emailData = {
        message: "Click the link below to go to your video.",
        buttontext: "View My Video",
        buttonlink: "http://sample.com/vids/" + ???????
    },
    body = EmailGenerator.generateHtml("actionEmailTemplate", emailData);
    Meteor.call("sendMailgunEmail",
                "account@sample.com",
                "sample",
                [Meteor.user().emails[0].address],
                "Your video is live!",
                body
    );
  }
});

来自Meteor Docs:

collection.insert(doc, [callback](

在集合中插入文档。返回其唯一_id。

因此,您可以通过将插入物存储在局部变量中来获取插入物中的_id

var videoId = Videos.insert({title: doc.title, userId: this.userId, date: new Date()});

查看 Meteor 文档以了解collection.insert

在集合中插入文档。返回其唯一_id。

只需将插入的返回值分配给稍后可以引用的变量:

//create a video
Meteor.methods({
  createVideo: function(doc) {
    var videoId = Videos.insert({title: doc.title, userId: this.userId, date: new Date()});
    var emailData = {
        message: "Click the link below to go to your video.",
        buttontext: "View My Video",
        buttonlink: "http://sample.com/vids/" + videoId
    },
    body = EmailGenerator.generateHtml("actionEmailTemplate", emailData);
    Meteor.call("sendMailgunEmail",
                "account@sample.com",
                "sample",
                [Meteor.user().emails[0].address],
                "Your video is live!",
                body
    );
  }
});

最新更新