title: Return multiple mongodb entries with the same attribute using express
body: <p>I'm trying to log all mongodb entries which are associated with a username using express. </p>
  
  <p>This is my code:</p>
  
  <pre><code>transcriptRepository.getTranscriptByUsername = (username) =&gt; {
      return Transcript.find({ username })
      .then( transcript =&gt; {
          console.log('ALL TRANSCRIPTS: ', transcript)
          return transcript
      })
  }
  </code></pre>
  
  <p>I'm sure there's supposed to be an array in there somewhere, but I don't know how to implement it.</p>
  
  <p>When I run that code using supertest, I get the following error message: </p>
  
  <blockquote>
    <p>Unhandled rejection CastError: Cast to string failed for value "{}" at
    path "username" for model "Transcript"
        at new CastError (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/error/cast.js:29:11)
        at castString (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/cast/string.js:34:9)
        at SchemaString.cast (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schema/string.js:445:10)
        at SchemaString.SchemaType.applySetters (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schematype.js:724:12)
        at SchemaString.SchemaType._castForQuery (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schematype.js:1113:15)
        at SchemaString.castForQuery (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schema/string.js:500:15)
        at SchemaString.SchemaType.castForQueryWrapper (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schematype.js:1082:15)
        at cast (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/cast.js:248:34)
        at model.Query.Query.cast (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/query.js:3710:12)
        at model.Query.Query._castConditions (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/query.js:1515:10)
        at model.Query.Query._find (/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/query.js:1530:8)
        at process.nextTick (/Users/annacuddeback/work/emblem-site/node_modules/kareem/index.js:333:33)
        at process._tickCallback (internal/process/next_tick.js:61:11)</p>
  </blockquote>
  
  <p>What's the best way to return multiple database entries with the same attribute? </p>
  
  <p>Edit: </p>
  
  <p>My <code>Transcript</code> model is as follows:</p>
  
  <pre><code>const Schema = mongoose.Schema
  const TranscriptSchema = new Schema({
  
    pdfContent: { 
      type: String,
      required: true,
      index: { unique: true }
    },
  
    hashValue: { //hash of transcript pdf contents
      type: String, 
      required: true,
    },
  
    username: { //matches an email in users, used to see who issued the transcript hash
      type: String,
      required: true
    },
  
    studentUsername: { //username of the student the transcript belongs to
      type: String,
      required: true
    },
  
    schoolID: {
      type: String, 
      required: true
    },
  
    sequence: Number,
    updatedAt: { type: Date, default: Date.now }
  })
  </code></pre>
  
  <p>My database is set up from front to back as server.js-->controller-->service--repository</p>
  
  <p>My server route is:</p>
  
  <pre><code>app.get('/transcript/query/username/:username', userController.getTranscriptByUsername) //gets transcripts by username
  </code></pre>
  
  <p>My controller function is:</p>
  
  <pre><code>userController.getTranscriptByUsername = (req, res) =&gt; {
      userService.getTranscriptByUsername(req.body)
      .then( (transcript) =&gt; {
          res.end(transcript.hashValue)
      })
  }
  </code></pre>
  
  <p>My service function is: </p>
  
  <pre><code>userService.getTranscriptByUsername = (username) =&gt; {
      return transcriptRepository.getTranscriptByUsername(username)
  }
  </code></pre>
  
  <p>My supertest unit test is:</p>
  
  <pre><code>it('should return 200 for getting transcripts', function(done) { //this is how mocha expects HTTP requests to be written: with a done parameter to the function
      request(server).get('/transcript/query/username/euler@python.com').expect(200, done)
  })
  </code></pre>