【问题标题】:Mongoose populate in MEAN stack not working猫鼬填充在 MEAN 堆栈中不起作用
【发布时间】:2015-09-17 16:04:42
【问题描述】:

我一直在阅读文档以及其他 SO 问题,但我真的找不到我做错了什么,即使查看与我的代码几乎相同的问题,那里提出的解决方案也不起作用。

相关代码:

用户架构(身份验证使用它):

mongoose = require('mongoose')

Schema = mongoose.Schema

UserSchema = new Schema(
  name:
    type: String
    required: true
  password:
    type: String
    required: true
  admin:
    type: Boolean
    default: false
  apiKey:
    type: String
    required: true
)

User = mongoose.model('User', UserSchema)

module.exports = User

报告架构:

mongoose = require('mongoose')

Schema = mongoose.Schema

ReportSchema = new Schema(
  # ...
    votes: [{
    user:
      type: Schema.Types.ObjectId
      ref: 'User'
    text:
      type: String
      default: ''}]
# ...
)

Report = mongoose.model('Report', ReportSchema)

module.exports = Report

用投票更新报告(req.user 来自认证系统):

  query =
    _id: req.params.id

  vote = req.body

  params =
    $push:
      votes:
        user: req.user._id
        text: vote.text or ''

  Report.findOneAndUpdate query, params, (err, result) ->
    return res.status(500).send(err) if err
    return res.status(404).end() unless result
    res.send result

查看报告:

# ...
if (req.params.id)
    Report.findOne({_id: req.params.id}).populate('votes.user').exec (err, report) ->
      return res.status(404).end() unless report
      res.send report
# ...

测试套件:

Report = mongoose.model "Report"
User = mongoose.model "User"

# ...
    it "add a vote with a blank comment", (done) ->
      Report.findOne title: 'test', (err, report) ->
          return done(err) if err
          request(app).put("#{apiUrl}/#{report._id}/vote").set("Authorization", "Bearer testKey").end (err, res) ->
            return done(err) if err
            expect(res.statusCode).toBe 200
            Report.findOne title: 'test', (err, report) ->
              console.log report
              expect(report.votes?.length).toBe 1
              expect(report.votes[0]?.text).toEqual ''
              expect(report.votes[0]?.user?.name).toEqual 'test'
              done()

登录到控制台的对象是什么样子的:

报告(应该填充投票数组中的用户字段):

{ _id: 5592d13e35c84be816000006,
  ...
  __v: 0,
  votes:
   [ { user: 5592d14235c84be816000015,
       _id: 5592d14335c84be816000017,
       text: '' } ],
  ... 
}

用户:

{ _id: 5592d14235c84be816000015,
  apiKey: 'testKey',
  password: 'testPassword',
  name: 'test',
  __v: 0,
  admin: false }

如果有人可以帮助我提供一些指导或指出错误,我将非常感激

提前谢谢你们!

【问题讨论】:

  • 我没有看到您在报表架构中的查询中使用的“标题”字段,但我假设该字段存在并且集合有一个标题值设置为测试的文档?在您的测试套件中,记录您在请求中发送的 report._id 并检查它是否与您期望的匹配
  • @SasikanthBharadwaj 是的,正如您猜想的那样,该字段存在,我想跳过不相关的内容并使代码示例尽可能简短。最后它变得更简单了:我正在检查的报告是直接通过猫鼬而不是通过我的 API 查找的,这就是为什么它没有被填充.....非常感谢你的时间,我实际上发现了错误在做你说的时候
  • @CyborgFish 请发布已检测到问题的答案。
  • @ZeMoon 不错的电话,我一回到家就会这样做,以防它帮助某人,但正如我所说的那样,这只是一个愚蠢的错误

标签: node.js mongodb coffeescript mongoose


【解决方案1】:

找到了解决方案,这是一个简单的错误:我正在检查的报告是直接通过猫鼬而不是通过我的 API 查找的,这就是它没有被填充的原因。正确的代码是:

Report.findOne title: 'test', (err, report) ->
      return done(err) if err
      request(app).put("#{apiUrl}/#{report._id}/vote").set("Authorization", "Bearer testKey").end (err, res) ->
        return done(err) if err
        expect(res.statusCode).toBe 200
        request(app).get("#{apiUrl}/#{report._id}").set("Authorization", "Bearer testKey").end (err, res) ->
          report = res.body
          expect(report.votes?.length).toBe 1
          expect(report.votes[0]?.texto).toEqual ''
          expect(report.votos[0]?.user?.name).toEqual 'test'
          done()

使用填充的 API 报告查找工作正常

【讨论】:

    猜你喜欢
    • 2016-12-24
    • 2018-06-10
    • 2014-12-19
    • 2013-06-20
    • 1970-01-01
    • 2015-08-16
    • 2017-01-10
    • 2014-05-01
    • 2021-02-03
    相关资源
    最近更新 更多