【问题标题】:Mongoose Sandbox: TypeError: Cannot read property 'length' of undefinedMongoose Sandbox:TypeError:无法读取未定义的属性“长度”
【发布时间】:2019-08-18 14:07:55
【问题描述】:

当我运行 node mongoose_sandbox.js 时,我收到以下错误:

...\node_modules\kareem\index.js:51 } else if (pre.fn.length > 0)

TypeError:无法读取未定义的属性“长度”

'use strict';

// mongoose setup
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/sandbox");
const db = mongoose.connection;

// error handling
db.on("error", (err) => {
    console.error("connection error:", err)
});

db.once("open", () => {
    console.log("db bağlandı, haberin olsun");
    // database work
    const Schema = mongoose.Schema; 
    const AnimalSchema = new Schema({
        type: {type: String, default: "goldfish"},
        size: String,
        color: {type: String, default: "golden"},
        mass: {type: Number, default: "0.007"},
        name: {type: String, default: "Angela"}
    });

AnimalSchema.pre("save"), function (next) {
    if (this.mass >= 100) {
        this.size = "büyük";
    } else if (this.mass >=5 && this.mass < 100) {
        this.size = "orta";
    } else {
        this.size = "küçük";
    }
    next();
};

const Animal = mongoose.model("Animal", AnimalSchema);

const elephant = new Animal({
    type: "fil",
    color: "gri",
    mass: 6000,
    name: "Kutay"
});

const animal = new Animal({});
const whale = new Animal({
    type: "whale",
    mass: 190500,
    name: "Enes"
});

const 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"
    },
    elephant,
    animal,
    whale
];

Animal.remove({}, (err) => {
    if (err) console.error(err);
    Animal.create(animalData, (err, animals) => {
        if (err) console.error(err);
        Animal.find({}, (err, animals) => {
            animals.forEach((animal) => {
                if (err) console.error(err);
                console.log(animal.name + " the " + animal.color + " " + animal.type + " is a " + animal.size + "-sized animal.");
            });
            db.close(() => {
                console.log("Bağlantıyı kapattım hadi..");
            });
        });
    });        
});

});

这是 Mongoose 教育,我在本地使用它。我希望看到 pre.save 方法起作用,但我无法得到结果。

【问题讨论】:

  • 你到底想在问题中说什么,应用程序甚至没有开始给你 pre.fn.length 错误..我在任何地方都看不到 pre.fn.length 代码行在你分享的这段代码 sn-p 中.. 更新问题

标签: mongodb mongoose


【解决方案1】:

这是因为您在保存单词后将括号放在错误的位置。 AnimalSchema.pre("save") 应该是

AnimalSchema.pre("save", function (next) {
    if (this.mass >= 100) {
        this.size = "büyük";
    } else if (this.mass >=5 && this.mass < 100) {
        this.size = "orta";
    } else {
        this.size = "küçük";
    }
    next();
});

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2020-09-14
    • 2018-01-11
    相关资源
    最近更新 更多