我正在按照本教程https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd来构建我的第一个api。
一切正常,然后我决定将其更改为存储位置和收藏夹,创建了FavoritesModel.js和LocationsModel.js。
我的server.js现在有
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'})
});
但是,当我运行npm run start
时,我会MissingSchemaError: Schema hasn't been registered for model "Tasks".
我做错了什么?任何地方都没有任何参考任务。我是否需要重建API或其他什么?我已经做了npm rebuild
。
答案 0 :(得分:0)
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
您需要两次这些模型。 连接前删除这些要求。和连接后的句子变量。