【问题标题】:nodejs: complaining about an used modelnodejs:抱怨使用过的模型
【发布时间】:2018-01-25 08:46:16
【问题描述】:

我正在按照本教程 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

【问题讨论】:

  • 我删除了那个,没有任务模式了。

标签: node.js mongodb express mongoose


【解决方案1】:
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

您需要这些模型两次。 在连接之前删除这些要求。以及连接后的句子变量。

【讨论】:

  • 谢谢,但现在我收到了Error: Route.post() requires callback functions but got a [object Undefined]
  • @SuperMonteiro 这是一个不同的问题,与这个问题无关。但是如果你在这里发布你的 Route.post 代码,我会帮你的。
  • 就是这样,我根本没有写任何 Route.post()。
  • @SuperMonteiro './api/routes/geolocationAPIRoutes' 里面有什么?
  • ''使用严格'; module.exports = function(app) { var geolocation = require('../controllers/geolocationAPIController'); app.route('/api/places') .get(geolocation.list_all_locations); //.post(geolocation.create_a_location); app.route('/api/places/:location') .get(geolocation.read_a_location) .post(geolocation.post_a_location) .put(geolocation.update_a_location); app.route('/api/places/favorites') .get(geolocation.list_all_favorites) .post(geolocation.post_a_favorite) .delete(geolocation.delete_a_favorite); };' @zerocho
猜你喜欢
  • 2015-07-02
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多