javascript - simple node api request don't display any data -


look have request in server.js file

var post = require('./../models/post'); //get posts app.get('/api/posts', function (req, res) {     post.getposts(function (err, posts) {         if(err) {             throw err;         }         res.json(posts);     }); }); 

and post.js model looks this:

var mongoose = require('mongoose');  var postschema = mongoose.schema({     username: {         type: string,         required: true     },     body: {         type: string,         required: true     },     date: { type: date,         default: date.now     } });  var post = module.exports = mongoose.model('post', postschema);  // posts module.exports.getposts = function (callback, limit) {     post.find(callback).limit(limit); }; 

in eye code written right not display data double check mongodb if have record there:

> show dbs admin      0.000gb bookstore  0.000gb local      0.000gb ownfb      0.000gb > use ownfb switched db ownfb > show collections posts > db.posts.find() { "_id" : objectid("597aa5b04c08c647b4efb58d"), "type" : "user", "body" : "post_z_mongoose_yo" } 

mongodb looks , contains 1 record why when go url http://localhost:5000/api/posts

it shows nothing except empty array

[]

also not error in cmd/browser.

gist full code of 2 files:

server.js: https://gist.github.com/anonymous/9b04527e97e889dcaa109f3ff459a5da

post.js: https://gist.github.com/anonymous/e77064ae71b5ef6d5a9abfd897187ddf

you're not passing in correct parameters getposts() function. it's expecting callback , limit... bet it's using 0 limit since you're not giving any.

or

you try having 1 export. postschema.getposts() attach method , export mongoose.model('post', postschema); , nothing else.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -