我试图启动我的服务器,但由于发生了TypeError,它无法正常工作,如下所示:
TypeError: Status.methods is not a function
at Object.<anonymous> (/Users/elmo/Documents/AndroidApp-Projekte/AbHofAPI/routes/api.js:9:8)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/elmo/Documents/AndroidApp-Projekte/AbHofAPI/server.js:15:17)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
[nodemon] app crashed - waiting for file changes before starting...
我的代码如下:
//dependencies:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
//connect to mongoDB
mongoose.connect('mongodb://mydbuser:mysecretpw@ds125914.mlab.com:25914/abhofdata');
//express:
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//routes:
app.use('/api', require('./routes/api'));
//start server:
app.listen(1000);
console.log('Server is running on port 1000');
//dependencies:
var express = require('express');
var router = express.Router();
//get models:
var Status = require('../models/status');
//routes:
Status.methods(['get', 'post', 'put', 'delete']);
Status.register(router, '/status');
//return router:
module.exports = router;
该类包含Schema,它概述了数据在mongoDB中的存储方式。
//dependencies:
var restful = require('node-restful');
var mongoose = restful.mongoose;
//Schema:
var statusSchema = new mongoose.Schema({
name: String,
strasse: String,
ort: String,
lat: String,
lng: String,
info: String
});
//return models:
module.export = restful.model('tblstatus', statusSchema);
也许文件夹结构对解决方案很重要。这里是:
AbHofAPI
│ package.json
│ server.js
│
└───node_modules
│
└───models
│ │ status.js
│
└───routes
│ api.js
答案 0 :(得分:2)
module.export = ..
你需要
module.exports = ..
答案 1 :(得分:0)
您正在使用猫鼬模型对象
Status.methods(['get', 'post', 'put', 'delete']);
Status.register(router, '/status');
也许您的意思是:
express.methods(['get', 'post', 'put', 'delete']);
express.register(router, '/status');