列出仅显示ID的问题

时间:2018-12-01 10:35:52

标签: javascript node.js mongodb

我的issue.model.js文件如下:

import mongoose from 'mongoose'
const IssueSchema = new mongoose.Schema({
  github_id: {
    type: String,
    description: 'The GitHub id of the issue'
  },
  issue_title: {
    type: String,
    description: 'The title of the issue'
  },
  issue_number: {
    type: String,
    description: 'The GitHub or Stackoverflow issue number'
  },
  issue_url: {
    type: String,
    description: 'The GitHub or Stackoverflow URL'
  },
  issue_state: {
    type: String,
    description: 'The current status of the issue on GitHub or Stackoverflow'
  }
})

export default mongoose.model('Issue', IssueSchema)

我在issue.routes.js中设置了此路由:

router.route('/api/issues')
  .get(authCtrl.requireSignin, issueCtrl.list)

我的issue.controller.js是这样的:

import Issue from '../models/issue.model'
import _ from 'lodash'
import errorHandler from './../helpers/dbErrorHandler'


const list = (req, res) => {
  Issue.find((err, issues) => {
    if (err) {
      return res.status(400).json({
        error: errorHandler.getErrorMessage(err)
      })
    }
    res.json(issues)
  }).select('Something not right')
}

export default {
  list
}

当我在端点GET上执行localhost:3000/api/issues时,我希望看到类似的东西:

{
    "_id" : ObjectId("5c024d774bf16a46c4cf4155"),
    "github_id" : "123456789",
    "issue_title" : "test issue 2",
    "issue_number" : "2",
    "issue_url" : "https://api.github.com/repos/Org/test/issues/2",
    "issue_state" : "open",
    "__v" : 0
}

相反,我得到了:

{
    "_id": "5c024d774bf16a46c4cf4155"
}

我正在使用失眠症进行检查。路线似乎还可以,因为我至少获得了正确的ID。我已经将控制器和模型与类似示例进行了比较,但看不到我所缺少的内容。谁能告诉我我哪里出问题了?

1 个答案:

答案 0 :(得分:1)

更改Issue.find((err, issues) => {

Issue.find({}, 'github_id issue_title issue_number issue_url issue_state',(err, issues) => {

这样,您可以包括更多字段。 请参阅文档中的示例:https://mongoosejs.com/docs/api.html#model_Model.find