Validation for duplicate doc breaks my meteor method


#1

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

#2

Do you see any error at the server console? the error you shared is from the client side…


#3

The server console output is

Exception while invoking method 'grammarQuestions.insert' undefined

But I don’t know what is undefined.


#4

I’m not sure about the syntax for the validate function of the collection schema.

From the docs, I see that you need:

MyCollection.simpleSchema().validate(doc);

But you’ve GrammarQuestions.simpleSchema().validator so I’m suspecting that validator is undefined.

I don’t use this syntax, can you double check if it actually works? you can place the validation somewhere in the code instead of the method validate param.


#5

I tested and that’s not it, I remove the if block with the duplicate check and the method works fine.