TypeError: res.cc is not a function at C:\Users\Administrator\Desktop\study\NodeJs\Demo\app.js:36:9 at Layer.handle_error (C:\Users\Administrator\Desktop\study\NodeJs\Demo\node_modules\express\lib\router\layer.js:71:5) at trim_prefix (C:\Users\Administrator\Desktop\study\NodeJs\Demo\node_modules\express\lib\router\index.js:326:13) at C:\Users\Administrator\Desktop\study\NodeJs\Demo\node_modules\express\lib\router\index.js:286:9 at Function.process_params (C:\Users\Administrator\Desktop\study\NodeJs\Demo\node_modules\express\lib\router\index.js:346:12) at next (C:\Users\Administrator\Desktop\study\NodeJs\Demo\node_modules\express\lib\router\index.js:280:10) at Layer.handle_error (C:\Users\Administrator\Desktop\study\NodeJs\Demo\node_modules\express\lib\router\layer.js:67:12) at trim_prefix (C:\Users\Administrator\Desktop\study\NodeJs\Demo\node_modules\express\lib\router\index.js:326:13) at C:\Users\Administrator\Desktop\study\NodeJs\Demo\node_modules\express\lib\router\index.js:286:9 at Function.process_params (C:\Users\Administrator\Desktop\study\NodeJs\Demo\node_modules\express\lib\router\index.js:346:12)
分析原因
TypeError: res.cc is not a function意思是找不到rec.cc()函数,那可能是:
Cannot set headers after they are sent to the client
报错内容
1
Cannot set headers after they are sent to the client
原因
原因是服务端给客户端响应多条信息,导致出错
1 2 3 4 5 6 7 8 9 10 11 12
db.query(sql,userinfo.username,(err,results)=>{ if (err) return res.cc(err) if (results.length > 0){ res.cc('用户名已被占用') // 第一次返回 } const addSql = `insert into tb_user set ?` db.query(addSql,{username:userinfo.username,password:userinfo.password},(err,results)=>{ if (err) return res.cc(err) if (results.affectedRows !== 1) return res.cc('注册失败,请稍候再试') res.cc('注册成功',0) // 第二次返回 }) })
解决问题
把多条信息响应修改一下,使服务端给客户端只响应一条信息即可。
1 2 3 4 5 6 7 8 9 10 11 12
db.query(sql,userinfo.username,(err,results)=>{ if (err) return res.cc(err) if (results.length > 0){ return res.cc('用户名已被占用') // 增加一个return,这样出错直接给客户端返回信息不往下执行 } const addSql = `insert into tb_user set ?` db.query(addSql,{username:userinfo.username,password:userinfo.password},(err,results)=>{ if (err) return res.cc(err) if (results.affectedRows !== 1) return res.cc('注册失败,请稍候再试') return res.cc('注册成功',0) }) })
‘ER_DATA_TOO_LONG: Data too long for column ‘‘password’’ at row 1’
报错内容
1
"ER_DATA_TOO_LONG: Data too long for column 'password' at row 1"
原因
数据库中password 写入的长度为300,但是password字段的长度只有200,因此报错
解决问题
将数据库中password字段的长度增大即可。
“Error: ‘expiresIn’ should be a number of seconds or string representing a timespan”
报错内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14
C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\mysql\lib\protocol\Parser.js:437 throw err; // Rethrow non-MySQL errors ^ Error: "expiresIn" should be a number of seconds or string representing a timespan at C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\jsonwebtoken\sign.js:52:15 at Array.forEach (<anonymous>) at validate (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\jsonwebtoken\sign.js:43:6) at validateOptions (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\jsonwebtoken\sign.js:58:10) at Object.module.exports [as sign] (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\jsonwebtoken\sign.js:141:5) at Query.<anonymous> (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\router_handler\user.js:71:30) at Query.<anonymous> (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\mysql\lib\Connection.js:526:10) at Query._callback (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\mysql\lib\Connection.js:488:16) at Query.Sequence.end (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24) at Query._handleFinalResultPacket (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\mysql\lib\protocol\sequences\Query.js:149:8)
“NodeJs报错 throw new typeerror(‘app.use() requires a middleware function’)”
错误
1 2 3 4 5 6 7 8 9 10 11 12 13 14
C:\Users\Administrator\Desktop\学习\NodeJs\day05\node_modules\express\lib\application.js:210 thrownewTypeError('app.use() requires a middleware function') ^
TypeError: app.use() requires a middleware function at Function.use (C:\Users\Administrator\Desktop\学习\NodeJs\day05\node_modules\express\lib\application.js:210:11 at Object.<anonymous> (C:\Users\Administrator\Desktop\学习\NodeJs\day05\03路由模块化.js:9:5) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) at internal/main/run_main_module.js:17:47 [nodemon] app crashed - waiting for file changes before starting...
Error: Cannot find module 'express' Require stack: - C:\Users\Administrator\Desktop\学习\NodeJs\day06\express写接口.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) at Function.Module._load (internal/modules/cjs/loader.js:746:27) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:93:18) at Object.<anonymous> (C:\Users\Administrator\Desktop\学习\NodeJs\day06\express写接口.js:2:17) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) { code: 'MODULE_NOT_FOUND', requireStack: [ 'C:\\Users\\Administrator\\Desktop\\学习\\NodeJs\\day06\\express写接口.js' ] } [nodemon] app crashed - waiting for file changes before starting...
分析错误
找不到express模块
看看导入模块有没有出错
看看package.json有没有下载包记录
发现package.json没有记录express,说明是是没有下载,那重新下载一下包就好了,npm i express
1
Error: Cannot find module 'express'
Syntax Error: TypeError: Cannot read properties of undefined (reading ‘parseComponent’)
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at newNodeError (internal/errors.js:322:7) at ServerResponse.setHeader (_http_outgoing.js:561:11) at ServerResponse.header (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\express\lib\response.js:771:10) at ServerResponse.send (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\express\lib\response.js:170:12) at ServerResponse.json (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\express\lib\response.js:267:15) at ServerResponse.send (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\express\lib\response.js:158:21) at Query.<anonymous> (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\router_handler\user.js:26:24) at Query.<anonymous> (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\mysql\lib\Connection.js:526:10) at Query._callback (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\mysql\lib\Connection.js:488:16) at Query.Sequence.end (C:\Users\Administrator\Desktop\学习\NodeJs\TestDemo\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24) { code: 'ERR_HTTP_HEADERS_SENT' } [nodemon] app crashed - waiting for file changes before starting...
解决前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 定义sql语句,查询用户名是否被占用 const sqlStr = 'select * from ev_users where username=?' db.query(sqlStr,[userinfo.username],(err,results)=>{ // 执行sql语句 if (err){ return res.send({status:1,msg:err.message}) }
// 判断用户名是否被占用 if (results.length > 0){ return res.send({status:1, msg:'用户名已被占用,请更换其他用户名'}) } // TODO : 用户名可用,其他操作