父范围中的函数的参数

时间:2016-01-25 20:03:25

标签: javascript angularjs meteor

我正在关注本教程http://www.angular-meteor.com/tutorials/socially/angular2/meteor-methods

所以:

import {Parties} from './parties';

function getContactEmail(user: Meteor.User): string {
    if (user.emails && user.emails.length)
        return user.emails[0].address;

    return null;
};

Meteor.methods({
    invite: function(partyId: string, userId: string) {
        check(partyId, String);
        check(userId, String);

        let party = Parties.findOne(partyId);

        if (!party)
            throw new Meteor.Error('404', 'No such party!');

        if (party.public)
            throw new Meteor.Error('400', 'That party is public. No need to invite people.');

        if (party.owner !== this.userId)
            throw new Meteor.Error('403', 'No permissions!');

        if (userId !== party.owner && (party.invited || []).indexOf(userId) == -1) {
            Parties.update(partyId, { $addToSet: { invited: userId } });

            let from = getContactEmail(Meteor.users.findOne(this.userId));
            let to = getContactEmail(Meteor.users.findOne(userId));

            if (Meteor.isServer && to) {
                Email.send({
                    from: 'noreply@socially.com',
                    to: to,
                    replyTo: from || undefined,
                    subject: 'PARTY: ' + party.name,
                    text: `Hi, I just invited you to ${party.name} on Socially.
                        \n\nCome check it out: ${Meteor.absoluteUrl()}\n`
                });
            }
        }
    }
});

invite(user: Meteor.User) {
        this.call('invite', this.party._id, user._id, (error) => {
            if (error) {
                alert(`Failed to invite due to ${error}`);
                return;
            }

            alert('User successfully invited.');
        });
    }

我无法理解(error) => {,错误值从哪里来?

1 个答案:

答案 0 :(得分:1)

ES6 (args) => {中的

是定义函数read more

的简写方式

所以

invite(user: Meteor.User) {
        this.call('invite', this.party._id, user._id, (error) => {
            if (error) {
                alert(`Failed to invite due to ${error}`);
                return;
            }

            alert('User successfully invited.');
        });
    }

相同
invite(user: Meteor.User) {
        this.call('invite', this.party._id, user._id, function(error){
            if (error) {
                alert(`Failed to invite due to ${error}`);
                return;
            }

            alert('User successfully invited.');
        });
    }