I am trying to send data from data.js file to mongoDB:
const data = {
users: [
{
name: 'MrAdmin',
email: 'admin@example.com',
password: bcrypt.hashSync('1234', 8),
isAdmin: true,
},
{
name: 'Tom',
email: 'tom@gmail.com',
password: bcrypt.hashSync('1234', 8),
idAdmin: false,
},
{
name: 'John ',
email: 'John77@gmail.com',
password: bcrypt.hashSync('1234', 8),
idAdmin: false,
},
],
}
user model:
const userSchema = new mongoose.Schema({
name: {type: String, required: true},
email: {type: String, required: true, unique: true},
password: {type: String, required: true},
isAdmin: {type: Boolean, default: false, required: true}
},{
timestamps: true,
});
server / api :
import express from 'express';
import mongoose from 'mongoose';
import productRouter from './routers/productRouter.js';
import userRouter from './routers/userRouter.js';
const app = express();
const uri = process.env.MONGODB_URL;
mongoose.connect(uri || 'mongodb://localhost:27017/e-commerce-shop', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}).then(() => console.log( 'Database Connected' ))
.catch(err => console.log( err ));;
app.use('/api/users', userRouter);
app.use('/api/products', productRouter);
app.get('/', (req, res)=>{
res.send('Working...');
});
app.use((err, req, res, next)=>{
res.status(500).send({message: err.message});
});
const port = process.env.PORT || 5000;
app.listen(port, ()=>{
console.log(`Server at http://localhost:${port}`);
});
user router:
import express from 'express';
import data from '../data.js';
import expressAsyncHandler from 'express-async-handler';
import User from '../models/userModel.js';
const userRouter = express.Router();
userRouter.get('/seed', expressAsyncHandler(async(req, res)=>{
//await User.remove({});
const createdUsers = await User.insertMany(data.users);
res.send({createdUsers});
}));
export default userRouter;
Data.js file has 3 elements inside users array. I can use just first and second user on my website. Moreover, mongoDb compass doesn't seem to get the third user (John). I have products section too. Metodolgy is completely the same as here and I can't get more than 2 elements as well(first and second product is visible for mongoDB compass and it is possible to render them, unlike the rest)
question from:
https://stackoverflow.com/questions/65647359/part-of-data-file-is-invisible-for-mongodb