使用express返回具有相同属性的多个mongodb条目

时间:2018-07-31 15:38:22

标签: javascript node.js mongodb express mongoose

我正在尝试使用express记录与用户名关联的所有mongodb条目。

这是我的代码:

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

我确定某个地方应该有一个数组,但是我不知道如何实现它。

当我使用supertest运行该代码时,出现以下错误消息:

  

未处理的拒绝CastError:在以下位置将值“ {}”转换为字符串失败   模型“字幕”的路径“用户名”       在新的CastError(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/error/cast.js:29:11)       在castString(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/cast/string.js:34:9)       在SchemaString.cast(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schema/string.js:445:10)       在SchemaString.SchemaType.applySetters(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schematype.js:724:12)       在SchemaString.SchemaType._castForQuery(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schematype.js:1113:15)       在SchemaString.castForQuery(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schema/string.js:500:15)       在SchemaString.SchemaType.castForQueryWrapper(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/schematype.js:1082:15)       在演员表上(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/cast.js:248:34)       在model.Query.Query.cast(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/query.js:3710:12)       在model.Query.Query._castConditions(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/query.js:1515:10)       在model.Query.Query._find(/Users/annacuddeback/work/emblem-site/node_modules/mongoose/lib/query.js:1530:8)       在process.nextTick(/Users/annacuddeback/work/emblem-site/node_modules/kareem/index.js:333:33)       在process._tickCallback(internal / process / next_tick.js:61:11)

返回具有相同属性的多个数据库条目的最佳方法是什么?

编辑:

我的Transcript模型如下:

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

我的数据库从前到后设置为server.js-> controller-> service--repository

我的服务器路由是:

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

我的控制器功能是:

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

我的服务功能是:

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

我的超级测试单元测试是:

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

2 个答案:

答案 0 :(得分:0)

您要在用户名中传递一个空对象,该函数需要一个字符串

未处理的拒绝CastError:在模型“ Transcript”的路径“用户名”处,对值“ {}” 的字符串转换为字符串失败

您必须检查您是否在传递字符串。

答案 1 :(得分:0)

此行看起来有问题:

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

我这样说是因为它被路由到userController.getTranscriptByUsername,并且您已经定义了该函数以接受用户名参数。但是Express回调函数需要requestresponse参数。

尝试将其更改为此:

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

此外,要从get请求中返回记录,您可以更改getTranscriptByUsername来接受回调作为第二个参数:

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

并再次更新Express功能:

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