我为猫鼬创建了一些文档但没有显示
I create some document to mongoose but not showing
我尝试在 mondoDB 中创建 5 个文档,我确定我成功创建了 5 个文档。
但在 Robo3T 上只显示一个文档。
这是我的代码
const Category = require('../category')
const db = require('../../config/mongoose')
const categoryList = require('./categoryList.json')
db.once('open', () => {
return Promise.all(Array.from({ length: 5 }, (_, index) => {
Category.create({
name: categoryList[index].name,
icon: categoryList[index].icon
})
}))
.then(() => console.log('categorySeeder.js is done'))
.then(() => process.exit())
})
这是我的 Robo3T
enter image description here
const Category = require('../category')
const db = require('../../config/mongoose')
const categoryList = require('./categoryList.json')
db.once('open', async () => {
return Promise.all(Array.from({ length: 5 }, (_, index) => {
return new Category({
name: categoryList[index].name,
icon: categoryList[index].icon
}).save();
// or
return Category.create({
name: categoryList[index].name,
icon: categoryList[index].icon
});
}))
.then(() => console.log('categorySeeder.js is done'))
.then(() => process.exit())
})
试试这个
使用aysnc/await
await Promise.all(Array.from({ length: 5}).map(async (_, index) => {
let categoryObj = new Category({ name: categoryList[index].name, icon: categoryList[index].icon });
categoryObj.save().then(res => console.log(`Created book ${res}`));
}));
我尝试在 mondoDB 中创建 5 个文档,我确定我成功创建了 5 个文档。
但在 Robo3T 上只显示一个文档。
这是我的代码
const Category = require('../category')
const db = require('../../config/mongoose')
const categoryList = require('./categoryList.json')
db.once('open', () => {
return Promise.all(Array.from({ length: 5 }, (_, index) => {
Category.create({
name: categoryList[index].name,
icon: categoryList[index].icon
})
}))
.then(() => console.log('categorySeeder.js is done'))
.then(() => process.exit())
})
这是我的 Robo3T enter image description here
const Category = require('../category')
const db = require('../../config/mongoose')
const categoryList = require('./categoryList.json')
db.once('open', async () => {
return Promise.all(Array.from({ length: 5 }, (_, index) => {
return new Category({
name: categoryList[index].name,
icon: categoryList[index].icon
}).save();
// or
return Category.create({
name: categoryList[index].name,
icon: categoryList[index].icon
});
}))
.then(() => console.log('categorySeeder.js is done'))
.then(() => process.exit())
})
试试这个
使用aysnc/await
await Promise.all(Array.from({ length: 5}).map(async (_, index) => {
let categoryObj = new Category({ name: categoryList[index].name, icon: categoryList[index].icon });
categoryObj.save().then(res => console.log(`Created book ${res}`));
}));