2
我有一個動物模式:貓鼬找到方法後,創建
const AnimalSchema = new mongoose.Schema({
type: { type: String, default: "goldfish" },
size: String,
color: { type: String, default: "golden" },
mass: { type: Number, default: 0.007 },
name: { type: String, default: "Angela" }
});
動物實驗數據數組:
let animalData = [
{
type: 'mouse',
color: 'gray',
mass: 0.035,
name: 'Marvin'
},
{
type: 'nutria',
color: 'brown',
mass: 6.35,
name: 'Gretchen'
},
{
type: 'wolf',
color: 'gray',
mass: 45,
name: 'Iris'
}
];
然後我試圖清空在動物模型中的所有數據,保存陣列數據庫,記錄一些動物實驗數據和關閉連接:
Animal
.remove({})
.then(Animal.create(animalData))
.then(Animal.find({}).exec())
.then(animals => {
animals.forEach(animal => console.log(`${animal.name} is ${animal.color} ${animal.type}`))
})
.then(() => {
console.log('Saved!');
db.close().then(() => console.log('db connection closed'));
})
.catch((err) => {
console.error("Save Failed", err);
});
但是,當我試圖執行此我得到一個錯誤: 保存失敗類型錯誤:animals.forEach是不是Animal.remove.then.then.then.animals功能 (C:_projects \ express_api \ mongoose_sandbox.js:89:12)
這有什麼錯我的代碼和如何使它工作?謝謝。