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)
})
Transcript? Also, how are you usinggetTranscriptByUsernamefunction in your test? - Pat Needham