2

I have a very simple resful api created just for practice. But when i try to use the url e.g. localhost:3000/people it only diplays an empty array like this [ ]. There is no error in console. I am using node-restful package to create the api. Here is the code i used :

Server.js (this is copied same to same from node-restful package(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"
  }
}

and inside my mongodb there is data in 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" }

Now when I start server and goto http://localhost/people it display an empty Array [ ]. But it should show the entry in Json format something like this

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

Please help!!! Please give me right direction. Thanks

1
  • Is anyone there ...who can answer this problem or give hint!!! Commented May 12, 2016 at 12:28

2 Answers 2

2

restful.model returns a Mongoose model and it, in turn, uses pluralized model name as a collection name. So in your case, people model references to peoples collection, which is empty. If you want to use mongoose naming algorithm properly, you can use person as the model name. Referenced mongoose collection will be people as you want it to be.

Update

Mongoose naming algorithm

As an example (mongoose have to be installed):

var utils = require('mongoose/lib/utils');
utils.toCollectionName('people'); // peoples
utils.toCollectionName('person'); // people
Sign up to request clarification or add additional context in comments.

2 Comments

I got your point thanks for early reply.. but i am not able to understand naming alogrithm.. in Mongod could you please give me a example with code where I can use people as model name and person as collection where I don't have to use pluralized nameing. Thanks in advance.. Though now changing the collection name to peoples made it work!!
Updated the post to give you an example how 'people' model goes to 'peoples' collection and 'person' goes to 'people' collection. Hope it helps.
1

Ok after a research I found the problem is with Mongoose which returns the pluralized form of collection name 'people' as 'peoples' or 'person' as 'persons'.. thats the reason behind the data to not get displayed. So I just forced it use the collection I want like this :

var mongoose = require('mongoose');

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

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

So here I have added the line {collection: 'person'} to force use this collection in my model. Now I can get the result as I needed from the exact collection I want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.