1
npm run
を実行するスクリプト(JS)を介してMongoDBデータベースにデータを入力しようとしています。私のMongooseデータシードスクリプトで何が問題になっていますか?
const mongoose = require('mongoose')
const dbConf = require('../config/database.js')
const User = require('../app/models/user.js')
const Account = require('../app/models/account.js')
mongoose.connect(dbConf.url, { useMongoClient: true })
mongoose.Promise = global.Promise
const users = [
{
_id: "58c039018060197ca0b52d4c",
email: "[email protected]",
password: "foo",
balance_cents: 100
},
{
_id: "58c03ada8060197ca0b52d52",
email: "[email protected]",
password: "foo",
balance_cents: 50000
}
]
const accounts = [
{
name: "Postbank",
iban: "1232423423",
swift: "2444444"
},
{
name: "DKB",
iban: "1234923423",
swift: "6667898"
},
{
name: "Fidor",
iban: "909873423",
swift: "998733"
}
]
async function dropDatabase() {
console.log('Removing User collection')
await User.remove()
console.log('Success!')
}
async function seedUsers() {
console.log('Seeding users..')
try {
await User.insertMany(users)
console.log('Success!')
} catch(error) {
console.log('Error:' + e)
}
}
dropDatabase()
seedUsers()
process.exit(0)
ただし、各機能のawait
ステートメントを過ぎているようには見えません。出力は:
Removing User collection
Seeding users..
データベースにはオブジェクトがありません。私はチュートリアルで見つけたのとまったく同じ構文を使用していますが、なぜこれが機能していないのか理解できません。 誰にでも分かりますか?私はそれが私が今見ていない非常にばかげたエラーだと思います。ありがとう!
tryを使用してデータベースの削除機能を実行できるのは、user.removeをキャッチするだけですか? – Sathish