0
votes

I'm trying to log all mongodb entries which are associated with a username using express.

This is my code:

transcriptRepository.getTranscriptByUsername = (username) => {
    return Transcript.find({ username })
    .then( transcript => {
        console.log('ALL TRANSCRIPTS: ', transcript)
        return transcript
    })
}

I'm sure there's supposed to be an array in there somewhere, but I don't know how to implement it.

When I run that code using supertest, I get the following error message:

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)

What's the best way to return multiple database entries with the same attribute?

Edit:

My Transcript model is as follows:

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 }
})

My database is set up from front to back as server.js-->controller-->service--repository

My server route is:

app.get('/transcript/query/username/:username', userController.getTranscriptByUsername) //gets transcripts by username

My controller function is:

userController.getTranscriptByUsername = (req, res) => {
    userService.getTranscriptByUsername(req.body)
    .then( (transcript) => {
        res.end(transcript.hashValue)
    })
}

My service function is:

userService.getTranscriptByUsername = (username) => {
    return transcriptRepository.getTranscriptByUsername(username)
}

My supertest unit test is:

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/[email protected]').expect(200, done)
})
2
Can you post the mongoose model you have for Transcript? Also, how are you using getTranscriptByUsername function in your test? - Pat Needham
@PatNeedham I appended my schema, route, and test to the original post - eulerspython

2 Answers

0
votes

You are passing a empty object in username, the function is expecting a string

Unhandled rejection CastError: Cast to string failed for value "{}" at path "username" for model "Transcript"

You have to check that you are passing a string.

0
votes

It is this line that looks problematic:

app.get('/transcript/query/username/:username', userController.getTranscriptByUsername) //gets transcripts by username

I say that because it gets routed to userController.getTranscriptByUsername, and you have that function defined to take a username argument. But the Express callback functions expect request and response arguments.

Try changing it to this:

app.get('/transcript/query/username/:username', (req, res) => {
  const {username} = req.params
  userController.getTranscriptByUsername(username)
})

Also, to return the transcripts from the get request, you could change getTranscriptByUsername to accept a callback as the second argument:

transcriptRepository.getTranscriptByUsername = (username, cb) => {
  Transcript.find({ username })
  .then( transcript => {
    console.log('ALL TRANSCRIPTS: ', transcript)
    cb(transcript)
  })
}

And to update the Express function one more time:

app.get('/transcript/query/username/:username', (req, res) => {
  const {username} = req.params
  userController.getTranscriptByUsername(username, transcript => {
    res.json({transcript}) // returning it as JSON in the response
  })
})