SuperMonteiro SuperMonteiro - 2 years ago 58
Node.js Question

nodejs: complaining about an used model

I was following this tutorial https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd for building my first api.

Everything worked fine, and then I decided to change it to store locations and favorites, created a FavoritesModel.js and a LocationsModel.js.

My server.js now has

var express = require('express'),
app = express(),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
Location = require('./api/models/LocationsModel'),
Favorite = require('./api/models/FavoritesModel'),
bodyParser = require('body-parser');

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/db');
require("./api/models/LocationsModel");
require("./api/models/FavoritesModel");


app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var routes = require('./api/routes/geolocationAPIRoutes');
routes(app);

app.listen(port);

console.log('geolocation RESTful API server started on: ' + port);

app.use(function(req, res) {
res.status(404).send({url: req.originalUrl + ' not found'})
});


However, when i run
npm run start
, i get
MissingSchemaError: Schema hasn't been registered for model "Tasks".


What am I doing wrong? There's no reference for Tasks anywhere anymore. Do I need to rebuild the API or something? I already did a
npm rebuild
.

Answer Source
Location = require('./api/models/LocationsModel'), // remove this
Favorite = require('./api/models/FavoritesModel'), // remove this
bodyParser = require('body-parser');

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/db');
var Location = require("./api/models/LocationsModel");// change this
var Favorite = require("./api/models/FavoritesModel");// change this

You requires those models twice. Remove those requires before connect. And sentence variables after connection.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download