I tried to start my server, but it do not work because a TypeError occurred, as you can see down below:
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...
My code is the following:
//dependencies:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
//connect to mongoDB
mongoose.connect('mongodb://mydbuser:[email protected]: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;
This Class contains the Schema, which outlines how the data is stored in 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);
Perhaps the folder structure is important for a solution. Here it is:
AbHofAPI
│ package.json
│ server.js
│
└───node_modules
│
└───models
│ │ status.js
│
└───routes
│ api.js
you did
module.export = ..
you need
module.exports = ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With