Javascript 使用express返回具有相同属性的多个mongodb条目
我正在尝试使用express记录与用户名关联的所有mongodb条目 这是我的代码:Javascript 使用express返回具有相同属性的多个mongodb条目,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我正在尝试使用express记录与用户名关联的所有mongodb条目 这是我的代码: transcriptRepository.getTranscriptByUsername = (username) => { return Transcript.find({ username }) .then( transcript => { console.log('ALL TRANSCRIPTS: ', transcript) return tr
transcriptRepository.getTranscriptByUsername = (username) => {
return Transcript.find({ username })
.then( transcript => {
console.log('ALL TRANSCRIPTS: ', transcript)
return transcript
})
}
我肯定那里应该有一个数组,但我不知道如何实现它
当我使用supertest运行该代码时,会收到以下错误消息:
未处理的拒绝CastError:对位于的值“{}”转换为字符串失败
模型“转录本”的路径“用户名”
在新的CastError(/Users/annacudeback/work/徽章site/node_modules/mongoose/lib/error/cast.js:29:11)
在castString(/Users/annacudeback/work/徽章site/node_modules/mongoose/lib/cast/string.js:34:9)
在SchemaString.cast(/Users/annacudeback/work/徽章site/node_modules/mongoose/lib/schema/string.js:445:10)
在schematring.SchemaType.applySetters(/Users/annacudeback/work/徽章site/node_modules/mongoose/lib/SchemaType.js:724:12)
在SchemaString.SchemaType.\u castForQuery(/Users/annacudeback/work/徽章site/node\u modules/mongoose/lib/SchemaType.js:1113:15)
在SchemaString.castForQuery(/Users/annacudeback/work/徽章site/node_modules/mongoose/lib/schema/string.js:500:15)
在SchemaString.SchemaType.castforqueryrapper(/Users/annacudeback/work/徽章site/node_modules/mongoose/lib/SchemaType.js:1082:15)
在cast(/Users/annacudeback/work/徽章site/node_modules/mongoose/lib/cast.js:248:34)
在model.Query.Query.cast(/Users/annacudeback/work/徽章site/node_modules/mongoose/lib/Query.js:3710:12)
在model.Query.Query.\u castConditions(/Users/annacudeback/work/徽章site/node\u modules/mongoose/lib/Query.js:1515:10)
在model.Query.Query.\u find(/Users/annacudeback/work/徽章site/node\u modules/mongoose/lib/Query.js:1530:8)
在process.nextTick(/Users/annacudeback/work/徽章site/node_modules/kareem/index.js:333:33)
在进程中。_tick回调(内部/process/next_tick.js:61:11)
返回具有相同属性的多个数据库条目的最佳方法是什么
编辑:
我的成绩单
模型如下:
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)
}
我的supertest单元测试是:
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)
})
您正在用户名中传递一个空对象,函数需要一个字符串 未处理的拒绝CastError:model“Transcript”的路径“username”处的值“{}”的转换为字符串失败
您必须检查是否正在传递字符串。这一行看起来有问题:
app.get('/transcript/query/username/:username', userController.getTranscriptByUsername) //gets transcripts by username
我之所以这么说,是因为它被路由到userController.getTranscriptByUsername
,并且您已经定义了该函数以获取用户名参数。但是快速回调函数需要request
和response
参数
尝试将其更改为:
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
})
})
你能把你的猫鼬模型贴在成绩单上吗?另外,您在测试中如何使用
getTranscriptByUsername
函数?@PatNeedham我将我的模式、路由和测试附加到原始的post中,这是有意义的,尽管如果是这样的话,我不确定我的其他单元测试为什么工作。我还有一些其他的单元测试也是从数据库中获取的,但它们都使用一个参数,比如:it('should return 200 for getting transcript',function(done){request(server).get('/transcript/hash value1')。expect(200,done)}
不过我会试试这个。但是,我在控制器和存储库之间还有一个服务文件。我将在原始帖子中链接来自两个控制器的相关代码。