【问题标题】:Restful api not displaying data from mongodbRestful api不显示来自mongodb的数据
【发布时间】:2016-09-08 03:57:51
【问题描述】:

我有一个非常简单的 resful api,只是为了练习。但是当我尝试使用网址时,例如localhost:3000/people 它只显示一个像 [ ] 这样的空数组。控制台没有错误。我正在使用 node-restful 包来创建 api。这是我使用的代码:

Server.js(这是从 node-restful 包复制到相同的相同(https://github.com/baugarten/node-restful

var express = require('express'),
    bodyParser = require('body-parser'),
    methodOverride = require('method-override'),
    morgan = require('morgan'),
    restful = require('node-restful'),
    mongoose = restful.mongoose;
var app = express();

app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({type:'application/vnd.api+json'}));
app.use(methodOverride());

mongoose.connect("mongodb://localhost/mydbs");

var people = app.people = restful.model('people', mongoose.Schema({
    name: String
  }))
  .methods(['get', 'post', 'put', 'delete']);

people.register(app, '/people');

app.listen(3000);
console.log("working");

package.json

{
  "name": "app1",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.15.1",
    "express": "^4.13.4",
    "lodash": "^4.12.0",
    "method-override": "^2.3.5",
    "mongoose": "^4.4.16",
    "morgan": "^1.7.0",
    "node-restful": "^0.2.5",
    "resourcejs": "^1.2.0"
  }
}

在我的 mongodb 中有数据在 db:named mydbs Collection:people

> show dbs
local  0.000GB
**mydbs  0.000GB**
test   0.000GB

> show collections
people

> db.people.find()
{ "_id" : ObjectId("57343f28f41d55c64cca135b"), "name" : "jackal" }

现在当我启动服务器并转到http://localhost/people 它显示一个空数组[]。但它应该以 Json 格式 显示类似这样的条目

{
  _v: 0,
 _id: 123344390dfsjkjsdf,
 name: 'jackal'
}

请帮忙!!!请给我正确的方向。谢谢

【问题讨论】:

  • 有没有人...谁可以回答这个问题或给出提示!!!

标签: json node.js mongodb rest express


【解决方案1】:

restful.model 返回一个 Mongoose 模型,然后它使用复数模型名称作为集合名称。因此,在您的情况下,people 模型引用了 peoples 集合,该集合是空的。 如果要正确使用猫鼬命名算法,可以使用person作为模型名称。引用的 mongoose 集合将是 people,如您所愿。

更新

Mongoose naming algorithm

例如(必须安装猫鼬):

var utils = require('mongoose/lib/utils');
utils.toCollectionName('people'); // peoples
utils.toCollectionName('person'); // people

【讨论】:

  • 我明白了你的意思,谢谢你的早期回复.. 但我无法理解命名算法.. 在 Mongod 中,你能给我一个代码示例,我可以使用人作为模型名称和人作为我不必使用复数命名的集合。在此先感谢.. 虽然现在将集合名称更改为 peoples 使它起作用了!!
  • 更新了帖子,为您提供了一个示例,“people”模型如何进入“peoples”集合,“person”如何进入“people”集合。希望对您有所帮助。
【解决方案2】:

好的,经过研究,我发现问题在于 Mongoose 将集合名称“people”的复数形式返回为“peoples”或“person”作为“persons”。这就是数据无法显示的原因。所以我只是强迫它像这样使用我想要的集合:

var mongoose = require('mongoose');

var PersonSchema = new mongoose.Schema({
    name: {
        type: String
    }
}, {collection: 'person'});

var person = mongoose.model('person', PersonSchema);
module.exports = person;

所以在这里我添加了行 {collection: 'person'} 来强制在我的模型中使用这个集合。现在我可以从我想要的确切集合中得到我需要的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2020-08-28
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    相关资源
    最近更新 更多