Mongodb Mongoose沙盒:TypeError:无法读取属性';长度';未定义的
当我运行node mongoose_sandbox.js时,出现以下错误: …\node\u modules\kareem\index.js:51 }否则如果(pre.fn.length>0) TypeError:无法读取未定义的属性“length”Mongodb Mongoose沙盒:TypeError:无法读取属性';长度';未定义的,mongodb,mongoose,Mongodb,Mongoose,当我运行node mongoose_sandbox.js时,出现以下错误: …\node\u modules\kareem\index.js:51 }否则如果(pre.fn.length>0) TypeError:无法读取未定义的属性“length” 'use strict'; // mongoose setup const mongoose = require('mongoose'); mongoose.connect("mongodb://localhost:27017/sandbox")
'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..");
});
});
});
});
“严格使用”;
//猫鼬设置
const mongoose=require('mongoose');
猫鼬。连接(“mongodb://localhost:27017/sandbox");
const db=mongoose.connection;
//错误处理
db.on(“错误”,(错误)=>{
console.error(“连接错误:”,err)
});
db.once(“打开”,()=>{
console.log(“db bağlandı,haberin olsun”);
//数据库工作
const Schema=mongoose.Schema;
const AnimalSchema=新模式({
类型:{type:String,默认值:“goldfish”},
大小:字符串,
颜色:{type:String,默认值:“golden”},
质量:{type:Number,默认值:“0.007”},
名称:{type:String,默认值:“Angela”}
});
AnimalSchema.pre(“保存”),函数(下一步){
如果(this.mass>=100){
this.size=“büyük”;
}否则如果(this.mass>=5&&this.mass<100){
this.size=“orta”;
}否则{
this.size=“küçük”;
}
next();
};
const Animal=猫鼬模型(“动物”,动物模式);
const大象=新动物({
类型:“fil”,
颜色:“gri”,
质量:6000,
姓名:“库泰”
});
const animal=新动物({});
康斯特鲸鱼=新动物({
类型:“鲸鱼”,
质量:190500,
名称:“Enes”
});
常量动物数据=[
{
键入:“鼠标”,
颜色:“灰色”,
质量:0.035,
姓名:“马文”
},
{
类型:“海狸鼠”,
颜色:“棕色”,
质量:6.35,
姓名:“格雷琴”
},
{
类型:“狼”,
颜色:“灰色”,
质量:45,
姓名:“Iris”
},
大象
动物
鲸鱼
];
动物。移除({},(err)=>{
if(err)控制台错误(err);
创建(动物数据,(呃,动物)=>{
if(err)控制台错误(err);
动物。查找({},(呃,动物)=>{
动物。forEach((动物)=>{
if(err)控制台错误(err);
console.log(animal.name+“the”+animal.color+“+animal.type+”是一个“+animal.size+”大小的动物。”);
});
db.close(()=>{
console.log(“Bağlantıyıkapattım hadi.”;
});
});
});
});
}))
这是一种猫鼬教育,我在本地使用。我希望看到pre.save方法起作用,但我无法得到结果。这是因为在单词save之后,您的括号放错了位置。 AnimalSchema.pre(“保存”) 应该是
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();
});
AnimalSchema.pre(“保存”,函数(下一步){
如果(this.mass>=100){
this.size=“büyük”;
}否则如果(this.mass>=5&&this.mass<100){
this.size=“orta”;
}否则{
this.size=“küçük”;
}
next();
});
你到底想要什么?在问题中,应用程序甚至没有给出pre.fn.length错误。。在您共享的代码段中,我看不到pre.fn.length代码行。。更新问题