SuperMonteiro SuperMonteiro -3 years ago 97
Node.js Question

nodejs: complaining about an used model

I was following this tutorial 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;

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

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


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;
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