I can’t seem to find an answer anywhere as to what I’m doing wrong here. I just want to wait for my for-loop to finish seeding my database before disconnecting
database.js
import mongoose from 'mongoose';
mongoose.connect(process.env.MONGO_URL || 'mongodb://localhost/db', { useNewUrlParser: true, useUnifiedTopology: true });
const exampleSchema = new mongoose.Schema({
name: String,
img: String,
});
const Example = mongoose.model('Example', exampleSchema);
export default Example;
seed.js
import axios from 'axios';
import mongoose from 'mongoose';
import Example from './database.js';
const seedExample = async () => {
const { data } = await axios.get(api placeholder);
for (let i = 0; i < data.length; i++) {
await Example.create({
name: data[i].name,
img: data[i].img,
}, (err, example) => {
if (err) return err
})
}
};
await seedExample();
mongoose.disconnect()
If I set a timeout then disconnect the documents are created but otherwise, it just closes before the function even completes. I’ve tried putting the disconnect at the end of the for loop as well.
Go easy on me I’m a noob, any advice all around very welcome. Thanks!