TypeError: signupTemplateCopy不是一个构造器,我的mongo数据库正在成功地与react应用程序连接。但是当我尝试从react中将数据插入到表中时,它显示了错误。我创建了模式signupTemplate,并通过const signupTemplateCopy = require('../models/models')将其导入到路由中,然后创建了signupeduser来保存来自user的数据。我认为问题出在const signedupUser = new signupTemplateCopy({ Name: request.body.Name, Email:request.body.email, Password: request.body.Password})的routes.js中。该程序将signupTemplateCopy作为构造函数。
server.js
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const routesURL = require('./routes/routes')
const cors = require('cors')
dotenv.config()
mongoose.connect(process.env.DATABASE_ACCESS, () => console.log("Database Connected"))
app.use(express.json())
app.use(cors())
app.use('/app', routesURL)
app.listen(4000, () => console.log('server is up and running on 4000'))models.js
const mongoose = require('mongoose')
const signupTemplate = new mongoose.Schema({
Name: {
type:String,
required:true
},
Email: {
type: String,
required: true
},
Password : {
type: String,
required: true
}
})
module.export=mongoose.model('mytable',signupTemplate)routes.js
const express = require('express')
const router = express.Router()
const signupTemplateCopy = require('../models/models')
router.post('/signup', (request, response) => {
const signedupUser = new signupTemplateCopy({
Name: request.body.Name,
Email:request.body.email,
Password: request.body.Password
})
signedupUser.save()
.then(data => {
response.json(data)
})
.catch(error =>{
response.json(error)
})
})
module.exports = routertest.http (通过发送请求进行测试)
POST http://localhost:4000/app/signup
Content-Type: application/json
{
"Name": "joey",
"Email": "abx@example.com",
"Password": "xyz@123"
}响应
HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 2037
Date: Wed, 09 Jun 2021 09:52:13 GMT
Connection: close
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>TypeError: signupTemplateCopy is not a constructor<br> at D:\E-commerce React\shopping\server\routes\routes.js:6:26<br> at Layer.handle [as handle_request] (D:\E-commerce React\shopping\server\node_modules\express\lib\router\layer.js:95:5)<br> at next (D:\E-commerce React\shopping\server\node_modules\express\lib\router\route.js:137:13)<br> at Route.dispatch (D:\E-commerce React\shopping\server\node_modules\express\lib\router\route.js:112:3)<br> at Layer.handle [as handle_request] (D:\E-commerce React\shopping\server\node_modules\express\lib\router\layer.js:95:5)<br> at D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:281:22<br> at Function.process_params (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:335:12)<br> at next (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:275:10)<br> at Function.handle (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:174:3)<br> at router (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:47:12)<br> at Layer.handle [as handle_request] (D:\E-commerce React\shopping\server\node_modules\express\lib\router\layer.js:95:5)<br> at trim_prefix (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:317:13)<br> at D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:284:7<br> at Function.process_params (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:335:12)<br> at next (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:275:10)<br> at cors (D:\E-commerce React\shopping\server\node_modules\cors\lib\index.js:188:7)</pre>
</body>
</html>发布于 2021-06-09 19:04:07
我花了点时间才弄明白。在您的mongoose模型中,您的module.export拼写不正确。
应为exports
https://stackoverflow.com/questions/67902083
复制相似问题