静态模式方法:无法读取未定义的 属性 'myFindOrCreate'
Static schema method: Cannot read property 'myFindOrCreate' of undefined
为了在我的 mongoose-Schemas 上运行,我喜欢创建静态方法,但我不得不承认,这是我第一次尝试启动 MEAN 堆栈后端 运行。前端和快速路由工作正常,所以我们首先关注 schemas:
// tracking.model.ts
import mongoose from 'mongoose';
var Schema = mongoose.Schema;
var TrackingModuleSchema = new Schema ({
// internal _id
modulename : { type: String, required:true,unique:true },
});
TrackingModuleSchema.statics.myFindByName= function (name,cb) {
return this.findOne( {name:new RegExp(name,'i')},cb);
};
TrackingModuleSchema.statics.myFindOrCreate = function (name,cb) {
this.myFindByName(name,function(err,modules) {
console.error(err);
console.log(modules);
cb(err,modules); // TODO return module._id....
});
};
// Create a `schema` for the Tracking object
var TrackingSchema = new Schema({
// _id internal.
timestamp: {type: Date },
sequence : { type: Number },
module: { type: Schema.Types.ObjectId, ref: 'TrackingModule' },
severity: { type: String},
action: { type: String },
from: { type: String },
to: { type: String },
});
// removed some code (Static methods to TrackingSchema)...
// create schema objects
var TrackingModule = mongoose.model('TrackingModule',TrackingModuleSchema);
var Tracking = mongoose.model('Tracking',TrackingSchema);
// module.exports all objects...
module.exports = function ( ) {
return {
TrackingModule : TrackingModule ,
Tracking : Tracking,
};
}
现在路由,其中导入和使用模式:
const express = require('express');
const router = express.Router();
import {Tracking,TrackingModule} from '../models/tracking.model';
router.get('/module', (req,res) => {
var moduleName = '';
if (req.query.moduleName) {
moduleName = req.query.moduleName;
} else {
console.log('Empty tracking-moduleName');
res.send(500);
return;
}
// the error happens in the following line:
TrackingModule.myFindOrCreate(moduleName,function(err,msgId){
if (err) {
console.error(err); // errors with tracking ->console...
res.send(500);
} else {
res.json( msgList );
}
});
});
// removed code for other gets and posts, not releveant to the error.
module.exports = router;
即使我尝试重命名静态函数的名称,也会出现错误 TypeError: Cannot read property 'myFindOrCreate' of undefined
。是的,我还没有完成 myFindOrCreate 的实施。
在此之前无法识别导入,所以我在 this instruction 之后安装了 babel-cli 6.23.0。我正在使用 package.json:
中的启动脚本
"scripts": {
"ng": "ng",
"start": "ng serve",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"starts": "nodemon server/server.js --watch server --exec babel-node",
"builds": "babel lib -d server.dist",
"serve": "node server.dist/server.js",
"tests": "mocha --compilers js:babel-register"
},
然而,构建、服务和测试我也没有成功,我稍后会检查。现在我已经检查了问题编辑器右侧的所有 "Similar Questions" 以及该编辑器上方的所有 "Questions that may already have your answer",没有任何帮助。
在你的 tracking.model.ts
文件中,而不是这个:
module.exports = function() {
return {
TrackingModule: TrackingModule,
Tracking: Tracking,
};
}
使用 export
关键字,如下所示:
export const TrackingModule = TrackingModule;
export const Tracking = Tracking;
您不能将旧的 module.exports
模式与 ES6 export-import
模式混淆。它们的处理方式不同。
为了在我的 mongoose-Schemas 上运行,我喜欢创建静态方法,但我不得不承认,这是我第一次尝试启动 MEAN 堆栈后端 运行。前端和快速路由工作正常,所以我们首先关注 schemas:
// tracking.model.ts
import mongoose from 'mongoose';
var Schema = mongoose.Schema;
var TrackingModuleSchema = new Schema ({
// internal _id
modulename : { type: String, required:true,unique:true },
});
TrackingModuleSchema.statics.myFindByName= function (name,cb) {
return this.findOne( {name:new RegExp(name,'i')},cb);
};
TrackingModuleSchema.statics.myFindOrCreate = function (name,cb) {
this.myFindByName(name,function(err,modules) {
console.error(err);
console.log(modules);
cb(err,modules); // TODO return module._id....
});
};
// Create a `schema` for the Tracking object
var TrackingSchema = new Schema({
// _id internal.
timestamp: {type: Date },
sequence : { type: Number },
module: { type: Schema.Types.ObjectId, ref: 'TrackingModule' },
severity: { type: String},
action: { type: String },
from: { type: String },
to: { type: String },
});
// removed some code (Static methods to TrackingSchema)...
// create schema objects
var TrackingModule = mongoose.model('TrackingModule',TrackingModuleSchema);
var Tracking = mongoose.model('Tracking',TrackingSchema);
// module.exports all objects...
module.exports = function ( ) {
return {
TrackingModule : TrackingModule ,
Tracking : Tracking,
};
}
现在路由,其中导入和使用模式:
const express = require('express');
const router = express.Router();
import {Tracking,TrackingModule} from '../models/tracking.model';
router.get('/module', (req,res) => {
var moduleName = '';
if (req.query.moduleName) {
moduleName = req.query.moduleName;
} else {
console.log('Empty tracking-moduleName');
res.send(500);
return;
}
// the error happens in the following line:
TrackingModule.myFindOrCreate(moduleName,function(err,msgId){
if (err) {
console.error(err); // errors with tracking ->console...
res.send(500);
} else {
res.json( msgList );
}
});
});
// removed code for other gets and posts, not releveant to the error.
module.exports = router;
即使我尝试重命名静态函数的名称,也会出现错误 TypeError: Cannot read property 'myFindOrCreate' of undefined
。是的,我还没有完成 myFindOrCreate 的实施。
在此之前无法识别导入,所以我在 this instruction 之后安装了 babel-cli 6.23.0。我正在使用 package.json:
"scripts": {
"ng": "ng",
"start": "ng serve",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"starts": "nodemon server/server.js --watch server --exec babel-node",
"builds": "babel lib -d server.dist",
"serve": "node server.dist/server.js",
"tests": "mocha --compilers js:babel-register"
},
然而,构建、服务和测试我也没有成功,我稍后会检查。现在我已经检查了问题编辑器右侧的所有 "Similar Questions" 以及该编辑器上方的所有 "Questions that may already have your answer",没有任何帮助。
在你的 tracking.model.ts
文件中,而不是这个:
module.exports = function() {
return {
TrackingModule: TrackingModule,
Tracking: Tracking,
};
}
使用 export
关键字,如下所示:
export const TrackingModule = TrackingModule;
export const Tracking = Tracking;
您不能将旧的 module.exports
模式与 ES6 export-import
模式混淆。它们的处理方式不同。