I set up a meteor method to check if the current item's title has been used but I'm not getting the alert I specified. Below is my "PortfolioItems" collection file.
PortfolioItems = new Mongo.Collection('portfolioItems');
Meteor.methods({
portfolioItemInsert: function(portfolioItemAttributes) {
check(Meteor.userId(), String);
check(portfolioItemAttributes, {
title: String
});
var portfolioItemWithSameTitle = PortfolioItems.findOne({ title: portfolioItemAttributes.title});
if (portfolioItemWithSameTitle) {
return {
portfolioItemExists: true,
_id: portfolioItemWithSameTitle._id
}
}
var user = Meteor.user();
var portfolioItem = _.extend(portfolioItemAttributes, {
userId: user._id,
submitted: new Date()
});
var portfolioItemId = PortfolioItems.insert(portfolioItem);
return {
_id: portfolioItemId
};
}
});
And the submit.js file:
Template.submit.events({
'submit #submit-form': function(e) {
e.preventDefault();
var portfolioItem = {
title: $(e.target).find('#submit-title').val()
};
Meteor.call('portfolioItemInsert', portfolioItem, function(error, result) {
if (error) {
return alert(error.reason);
}
if(result.portfolioItemWithSameTitle) {
alert('Title already taken!');
}
Router.go('portfolioItemPage', {_id: result._id});
});
}
});
PS. The item won't actually show up as duplicates, but the alert won't show stating it's a duplicate.
Aucun commentaire:
Enregistrer un commentaire