简体   繁体   中英

How to use mongoose schema to get data and send it to client?

I am new to nodejs so I have app that is connected with mongodb now i have api getAllDiagrams in router that should send all diagram data to client, The approach i am trying to implement is i want to call db from controller and once i have data i will require that in router and send it to client , Below code i could not make it work and getting error then is not defined . Any idea what is implemented wrong or any better way to use this approach.

app.js

var express = require('express');
var app = express();
var router = express.Router();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/develop-modeler');
require('./server/api/diagram/diagram.model.js');

mongoose.connection.on('error', function(err) {
    console.error('MongoDB connection error: ' + err);
});

app.use(express.static(path.join(__dirname, 'public')));

app.listen(8760, function() {
    console.log('I am listening 8760...');
})

diagram.controller.js

 var Diagram = require('./diagram.model');
    var mongoose = require('mongoose');

    module.exports = function index(req, res) {
  Diagram.find({}, function(err, result) {
    if (!err) {
      console.log('Response from controller', result);
      return res.send(result);
    }
  });
}

diagram.model.js

var mongoose = require('mongoose');

/*var User = require('../user/user.model.js');
var Group = require('../group/group.model.js');*/

var DiagramSchema = new mongoose.Schema({
  text: String,
  owner: {type: String, ref:'User'},
  groups: [{type: String, ref: 'Group'}],
  users: [{type: String, ref: 'User'}],
  string: String
});

 mongoose.model('Diagram', DiagramSchema);

router.js

var express = require('express');
var controller = require('./diagram.controller');

var router = express.Router();

console.log('THis is in router',controller.index());
router.get('/getAllDiagram',controller.index());

/*router.get('/', controller.index);*/

module.exports = router;

As I am seeing you are not using any promises stuff but expect to return a promise in your query,.But it is promise way of work that's why you are getting this error

 var express = require('express');
var controller = require('./diagram.controller');
var router = express.Router();
router.get('/getAllDiagram',controller.index());

module.exports = function index(req, res) {
  Diagram.find({}, function(err, result) {
    if (!err) {
      console.log('Response from controller', result);
      return res.send(result);
    }
  });
}

Update your schema like this

var mongoose = require('mongoose');

/*var User = require('../user/user.model.js');
var Group = require('../group/group.model.js');*/

var DiagramSchema = mongoose.Schema({
  text: String,
  owner: {type: String, ref:'User'},
  groups: [{type: String, ref: 'Group'}],
  users: [{type: String, ref: 'User'}],
  string: String
});

 module.exports=mongoose.model('Diagram', DiagramSchema);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM