{"action":"closed","issue":{"url":"https://api.github.com/repos/eggjs/egg/issues/2936","repository_url":"https://api.github.com/repos/eggjs/egg","labels_url":"https://api.github.com/repos/eggjs/egg/issues/2936/labels{/name}","comments_url":"https://api.github.com/repos/eggjs/egg/issues/2936/comments","events_url":"https://api.github.com/repos/eggjs/egg/issues/2936/events","html_url":"https://github.com/eggjs/egg/issues/2936","id":354202338,"node_id":"MDU6SXNzdWUzNTQyMDIzMzg=","number":2936,"title":"同一个api线上和线下的返回值不同","user":{"login":"williamnie","id":26871884,"node_id":"MDQ6VXNlcjI2ODcxODg0","avatar_url":"https://avatars1.githubusercontent.com/u/26871884?v=4","gravatar_id":"","url":"https://api.github.com/users/williamnie","html_url":"https://github.com/williamnie","followers_url":"https://api.github.com/users/williamnie/followers","following_url":"https://api.github.com/users/williamnie/following{/other_user}","gists_url":"https://api.github.com/users/williamnie/gists{/gist_id}","starred_url":"https://api.github.com/users/williamnie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/williamnie/subscriptions","organizations_url":"https://api.github.com/users/williamnie/orgs","repos_url":"https://api.github.com/users/williamnie/repos","events_url":"https://api.github.com/users/williamnie/events{/privacy}","received_events_url":"https://api.github.com/users/williamnie/received_events","type":"User","site_admin":false},"labels":[{"id":898498085,"node_id":"MDU6TGFiZWw4OTg0OTgwODU=","url":"https://api.github.com/repos/eggjs/egg/labels/Need%20Reproduce","name":"Need Reproduce","color":"75e7ef","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2018-08-27T06:57:54Z","updated_at":"2018-08-27T14:11:14Z","closed_at":"2018-08-27T14:11:14Z","author_association":"NONE","body":"* **Node Version**: v10.9.0\r\n* **Egg Version**: v2.9.1\r\n* **遇到的问题**:  同一个api线上和线下的返回值不同，逻辑为查询mongodb后返回数据，返回之前经过一个方法，将mongodb的_id 转换为id\r\n* **转换方法为**:\r\n`\r\nexports.transform = (data, config) => {\r\n  if (!data) {\r\n    return null;\r\n  }\r\n  const trans = (obj) => {\r\n    if (config && config.rename) {\r\n      config.rename.forEach((item) => {\r\n        obj[item.new] = obj[item.old];\r\n        delete obj[item.old];\r\n      });\r\n    }\r\n    if (config && config.remove) {\r\n      config.remove.forEach((item) => {\r\n        delete obj[item];\r\n      });\r\n    }\r\n    if (config && config.add) {\r\n      config.add.forEach((item) => {\r\n        obj[item.key] = item.value;\r\n      });\r\n    }\r\n  };\r\n\r\n\r\n  let obj = null;\r\n  if (Array.isArray(data)) {\r\n    obj = data.map((item) => {\r\n      return item.toJSON();\r\n    });\r\n    obj.forEach((item) => {\r\n      trans(item);\r\n    });\r\n\r\n  } else {\r\n    try {\r\n      obj = data.toJSON();\r\n    } catch (error) {\r\n      obj = data;\r\n    }\r\n    trans(obj);\r\n  }\r\n  return obj;\r\n};\r\n\r\n`\r\n* **调用方式和接收参数**:\r\n`\r\n// 调用方式\r\nconst data = await ctx.model.Source.find({ name: { $regex: query }, key }, 'checked name key');\r\nreturn ctx.helper.transform(data, config.transformConfig);\r\n// 接收参数\r\ndata 为mongodb查询出的数据，config配置如下 \r\n config.transformConfig = {\r\n    rename: [{ new: 'id', old: '_id' }],\r\n  };\r\n`\r\n* **调用api返回值为**:\r\n`\r\n线上：{\r\n    \"code\": 0,\r\n    \"data\": [\r\n        {\r\n            \"checked\": true,\r\n            \"name\": \"IEEE Workshop on Automatic Speech Recognition and Understanding\",\r\n            \"key\": \"paper\",\r\n            \"user_id\": \"5b7642601c3e28453d57f6cd\"\r\n        },\r\n    ],\r\n    \"success\": true\r\n}\r\n线下：{\r\n    \"code\": 0,\r\n    \"data\": [\r\n        {\r\n            \"checked\": true,\r\n            \"name\": \"IEEE Workshop on Automatic Speech Recognition and Understanding\",\r\n            \"key\": \"paper\",\r\n            \"id\": \"5b7642601c3e28453d57f6cd\"\r\n        },\r\n    ],\r\n    \"success\": true\r\n}\r\n`\r\n线上与线下返回以后，线上为user_id 字段，线下为id，线下没问题，不知道为什么线上会变成user_id,重启后会消失，但不一会又会出现。\r\n有人遇到过这个问题么？还有就是每次上线都需要把node_module的文件夹删除，重新yarn安装才可以。\r\n\r\n\r\n\r\n"}}
{"actor":{"display_login":"williamnie"}}