【问题标题】:node mongoose mocha how to debug a failing mongoose test?node mongoose mocha 如何调试失败的猫鼬测试?
【发布时间】:2017-09-30 13:43:49
【问题描述】:

与我之前对 Permissions 所做的类似,我正在尝试测试将 Privileges 添加到我的用户中...参数正常但 findOneAndUpdate 推送不会将其插入数组中...

型号

    const Schema = mongoose.Schema;

    const Privilege = new Schema({
      privilege: {
        table_id: {
          type: String,
          required: false
        },
        canWrite: {
          type: Boolean,
          required: true,
          default: false
        }
      }
    });

    const UserSchema = new mongoose.Schema({
      username: {
          type: String,
          required: true
      },
      ...
      privileges: [Privilege]
      ...

测试

      const newWritePrivilege = { privilege: { user_id: '56z787zzz67fc', canWrite: true }};

     .post(`/api/v1/users/${user._id}/privileges`)
             .send(newWritePrivilege)
             .expect(httpStatus.OK)

控制器

    function grantPrivilege(req, res, next) {
      console.log('GRANT PRIVILEGE: %j TO USER %j', req.body, req.params.userId);
      User.findOneAndUpdate({ _id: req.params.userId }, { $push: { privilege: req.body } }, { new: true })
        .then(savedUser => res.json(savedUser))
        .catch((e) => {
          console.log('ERROR: ', e);
          next(e);
        });
    }

在console.log中,我可以检查用户id是否正确,用户文档存在,但没有插入任何内容... 如何调试?

控制台

    GRANT PRIVILEGE: {"privilege":{"table_id":"56z787zzz67fc","canWrite":true}} TO USER "5908fe55aec3be291bc317b5"
    user: 
    {"__v":0,"username":"KK123","password":"KKpassword","email":"kk123@example.com","mobileNumber":"9999999999",
    "_id":"5908fe55aec3be291bc317b5","createdAt":"2017-05-02T21:47:01.795Z","privileges":[]}

感谢反馈

附加信息..

测试返回 200 但数组中没有任何内容

    res:     
         {"req"{"method":"POST","url":"http://127.0.0.1:65296/api/v1/users/5909015a9be435292a992bfc/privileges",
   "data":{"privilege":
  {"table_id":"56z787zzz67fc","canWrite":true}},"headers":{"user-agent":"node-superagent/3.5.2",
   "content-type":"application/json"}},"header":{"vary":"X-HTTP-Method-Override, Accept-Encoding","x-dns-prefetch-control":"off","x-frame-options":"SAMEORIGIN","x-download-options":"noopen","x-content-type-options":"nosniff","x-xss-protection":"1; mode=block","access-control-allow-origin":"*",
  "content-type":"application/json; charset=utf-8","content-length":"4","etag":"W/\"4-K+iMpCQsduglOsYkdIUQZQMtaDM\"",
  "date":"Tue, 02 May 2017 21:59:54 GMT","connection":"close"},
  "status":200,"text":"null"}

【问题讨论】:

    标签: node.js mongoose mocha.js


    【解决方案1】:

    架构:

    **privileges**: [Privilege]
    

    更新文档:

    { $push: { **privilege**: req.body } }
    

    复数与单数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-24
      • 2019-05-04
      • 2014-10-12
      • 2017-02-27
      • 2017-10-28
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      相关资源
      最近更新 更多