0

I have two forms (signup form and an other form XYZ) in one Reactjs/Nodejs project.

How to post the two forms data to two different databases table, each one autonome ?

PS: I'm new in Nodejs, Thanks in advance.

My codes so far:

server.js:

const express = require('express')
const mongoose = require('mongoose')
const bodyparser = require('body-parser')

const FormRoute = require('./routes/FormRoute')

//database
mongoose.connect('mongodb://localhost:27017/form', { useNewUrlParser: true, useUnifiedTopology: true })
const db = mongoose.connection

db.on('error', (err) => {
  console.log(err)
})
db.once('open', () => {
  console.log("Database connection established!")
})


//app
const app = express()

app.use(bodyparser.urlencoded({ extended: true }))
app.use(bodyparser.json())

//cors
const cors = require('cors')
app.use(cors())

//server run
const PORT = process.env.PORT || 5000
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
})

app.use('/api/form', FormRoute);

FormModel.js: (this is signup form model)

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const formSchema = new Schema({
    title: {
        type: String
    },
    fname: {
        type: String
    },
    lname: {
        type: String
    },
    email: {
        type: String
    },
    phoneNumber: {
        type: String
    },
    cv: {
        type: String
    },
    coverLetter: {
        type: String
    },
    score: {
        type: Number,
        default: 0
    }
}, { timestamps: true })

const form = mongoose.model('form', formSchema)

module.exports = form

FormXYZModel.js: (this is XYZ form model)

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const formXYZSchema = new Schema({
name: {
    type: String
},
email: {
    type: String
},
password: {
    type: String
},
dateOfBirth: {
    type: Date
},
role: {
    type: String,
    default: user
}, { timestamps: true })

const formXYZ = mongoose.model('formXYZ', formSchema)

module.exports = formXYZ
Ava
  • 512
  • 2
  • 26
  • check this out does this answer your question ? https://stackoverflow.com/a/19475270/12541413 – Sagar Darekar Jun 14 '22 at 17:01
  • @SagarDarekar I already checked that answer but to trust you i dont know how to tweak the answer to my problem – Ava Jun 14 '22 at 18:20

0 Answers0