【问题标题】:React mongodb error - cannot connect to mongodb table反应 mongodb 错误 - 无法连接到 mongodb 表
【发布时间】:2021-08-26 08:03:51
【问题描述】:

TypeError:signupTemplateCopy 不是构造函数 我的 mongo 数据库已成功连接到 react 应用程序。但是当我尝试从反应中将数据插入表中时,它显示错误。我创建了模式注册模板并通过const signupTemplateCopy = require('../models/models') 将其导入到路由中,然后我创建了注册用户来保存来自用户的数据。我认为问题出在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 = router

test.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> &nbsp; &nbsp;at D:\E-commerce React\shopping\server\routes\routes.js:6:26<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (D:\E-commerce React\shopping\server\node_modules\express\lib\router\layer.js:95:5)<br> &nbsp; &nbsp;at next (D:\E-commerce React\shopping\server\node_modules\express\lib\router\route.js:137:13)<br> &nbsp; &nbsp;at Route.dispatch (D:\E-commerce React\shopping\server\node_modules\express\lib\router\route.js:112:3)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (D:\E-commerce React\shopping\server\node_modules\express\lib\router\layer.js:95:5)<br> &nbsp; &nbsp;at D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:281:22<br> &nbsp; &nbsp;at Function.process_params (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:335:12)<br> &nbsp; &nbsp;at next (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:275:10)<br> &nbsp; &nbsp;at Function.handle (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:174:3)<br> &nbsp; &nbsp;at router (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:47:12)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (D:\E-commerce React\shopping\server\node_modules\express\lib\router\layer.js:95:5)<br> &nbsp; &nbsp;at trim_prefix (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:317:13)<br> &nbsp; &nbsp;at D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:284:7<br> &nbsp; &nbsp;at Function.process_params (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:335:12)<br> &nbsp; &nbsp;at next (D:\E-commerce React\shopping\server\node_modules\express\lib\router\index.js:275:10)<br> &nbsp; &nbsp;at cors (D:\E-commerce React\shopping\server\node_modules\cors\lib\index.js:188:7)</pre>
    </body>
    </html>

【问题讨论】:

    标签: reactjs mongodb express nodemon


    【解决方案1】:

    我花了一些时间才弄清楚这一点。您的 mongoose 模型中 module.export 的拼写不正确。

    应该是出口

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2016-07-31
      相关资源
      最近更新 更多