Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

javascript - part of data file is invisible for mongoDb

I am trying to send data from data.js file to mongoDB:

const data = {
    users: [
      {
          name: 'MrAdmin',
          email: '[email protected]',
          password: bcrypt.hashSync('1234', 8),
          isAdmin: true,
      },
      {
          name: 'Tom',
          email: '[email protected]',
          password: bcrypt.hashSync('1234', 8),
          idAdmin: false,
      },
      {
          name: 'John ',
          email: '[email protected]',
          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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...