错误提示:
(node:2636) UnhandledPromiseRejectionWarning: TypeError: User.findOne is not a function
at module.exports (D:\web\blog01\route\admin\user-modify-fn.js:8:27)
at Layer.handle [as handle_request] (D:\web\blog01\node_modules\express\lib\router\layer.js:95:5)
at next (D:\web\blog01\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\web\blog01\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\web\blog01\node_modules\express\lib\router\layer.js:95:5)
at D:\web\blog01\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\web\blog01\node_modules\express\lib\router\index.js:335:12)
at next (D:\web\blog01\node_modules\express\lib\router\index.js:275:10)
at Function.handle (D:\web\blog01\node_modules\express\lib\router\index.js:174:3)
at router (D:\web\blog01\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (D:\web\blog01\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\web\blog01\node_modules\express\lib\router\index.js:317:13)
at D:\web\blog01\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\web\blog01\node_modules\express\lib\router\index.js:335:12)
at next (D:\web\blog01\node_modules\express\lib\router\index.js:275:10)
at guard (D:\web\blog01\middleware\loginGuard.js:27:9)
(node:2636) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
错误原因:User.findOne is not a function
const User = require('../../model/user');
......
let user = await User.findOne({ _id: id });
require(’…/…/model/user’),获取的不是User对象,还包括其他对象,应改为:
const {User} = require('../../model/user');