Hello,
After I added an if to check for duplicates, my method stopped working.
How can I check for duplicates? The method is only for admins and there is only 1 admin so I don’t care about race conditions because only one user can add documents to the collection.
I have no idea what is causing the error, see the error at bottom of this post.
Thanks
export const insertQuestion = new ValidatedMethod({
name: 'grammarQuestions.insert',
validate: GrammarQuestions.simpleSchema().validator({ clean: true, filter: true }),
run(document) {
if (!this.userId) {
throw new Meteor.Error('grammarQuestions.insert.notLoggedIn',
'Must be logged in to insert question.');
}
if (!(/\b______\b/g).test(document.desc)) {
throw {
error: 'validation-error',
details: [{
type: 'blankRequired',
name: 'desc',
}]
};
}
if (document.options.filter(option => option.correct).length != 1) {
throw {
error: 'validation-error',
details: [{
type: 'selectCorrectOption',
name: 'options',
}]
};
}
let docs = GrammarQuestions.find({desc: document.desc}).fetch();
if (docs.length > 0) {
throw {
error: 'validation-error',
details: [{
type: 'duplicateQuestion',
name: 'desc',
}]
};
return;
}
let currentDate = new Date();
document.userId = this.userId;
document.createdAt = currentDate;
document.updatedAt = currentDate;
document.desc = document.desc.replace(/’/g, "'");
document.desc = document.desc.replace(/\*/g, "");
return GrammarQuestions.insert(document);
},
});
1. rrorClass {isClientSafe: true, error: 500, reason: "Internal server error", details: undefined, message: "Internal server error [500]", …}
1. details:undefined
2. error:500
3. errorType:"Meteor.Error"
4. isClientSafe:true
5. message:"Internal server error [500]"
6. reason:"Internal server error"
7. stack:"Error: Internal server error [500]↵ at Connection._livedata_result (http://localhost:8000/packages/ddp-client.js?hash=69c1d15adcf9b913cb4704b652adeff4bc462aa8:1975:25)↵ at Connection.onMessage (http://localhost:8000/packages/ddp-client.js?hash=69c1d15adcf9b913cb4704b652adeff4bc462aa8:2169:14)↵ at http://localhost:8000/packages/socket-stream-client.js?hash=7063aa833005aff7419b3f8c222e877582f1a3e0:262:13↵ at Array.forEach (<anonymous>)↵ at ClientStream.forEachCallback (http://localhost:8000/packages/socket-stream-client.js?hash=7063aa833005aff7419b3f8c222e877582f1a3e0:353:33)↵ at SockJS.socket.onmessage (http://localhost:8000/packages/socket-stream-client.js?hash=7063aa833005aff7419b3f8c222e877582f1a3e0:261:18)↵ at SockJS.REventTarget.dispatchEvent (http://localhost:8000/packages/socket-stream-client.js?hash=7063aa833005aff7419b3f8c222e877582f1a3e0:641:22)↵ at SockJS._dispatchMessage (http://localhost:8000/packages/socket-stream-client.js?hash=7063aa833005aff7419b3f8c222e877582f1a3e0:1820:10)↵ at SockJS._didMessage (http://localhost:8000/packages/socket-stream-client.js?hash=7063aa833005aff7419b3f8c222e877582f1a3e0:1886:16)↵ at WebSocket.that.ws.onmessage (http://localhost:8000/packages/socket-stream-client.js?hash=7063aa833005aff7419b3f8c222e877582f1a3e0:2044:15)"
8. __proto__:Error