2017-02-26 24 views
1

要操作我的mongoose-Schemas,我喜歡創建靜態方法,但我不得不承認,這是我第一次嘗試獲取MEAN-stack-backend啓動並運行。前端和快速路由工作正常,所以我們專注於模式第一:靜態模式方法:無法讀取未定義的屬性'myFindOrCreate'

// 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的實現。 在此之前,導入不被識別,所以我已經安裝了babel-cli 6.23.0以下this instruction。我使用的是開始,skript從我的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" 
    }, 

然而,建立,服務和測試中,我沒能擦出火花,我會以後檢查。現在我已經檢查了問題編輯器右側的所有「類似問題」以及編輯器上方的所有「可能已經有答案的問題」,沒有任何幫助。

回答

1

在你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模式。它們的處理方式不同。

+1

我必須將'var TrackingModule ='已經更改爲'export const TrackingModule = mongoose.model ...'。感謝提供正確方向的重要提示。 – Myonara

相關問題