4

I'm getting started with the MEAN STACK, so I took their project (about posting articles), and I'm trying to costomize it in order to get the list of all flows that i can filter with angularjs and also findOne by id. I followed the same thing that they did for articles to create JS files related to flows (flow is my object). So I have a collection named flows that I imported to the same db used by the MEAN STACK (db == mean-dev) and I tryed this code in:

// myApp/serves/models/flow.js

'use strict';
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

// Flow Schema
var FlowSchema = new Schema({
    _id: {
        type: Number,
        default: ''
    },
    name: {
        type: String,
        default: '',
        trim: true
    },
    Clients: {
        type: Array,
        default: '',
        trim: true
    },
    DP Prot: {
        type: String,
        default: ''
    }
    /* And 15 others attributes...*/
}); 

/** Statics */
FlowSchema.statics.load = function(id, cb) {
    this.findOne({
        _id: id
    }).exec(cb);
};
// Define the collection
mongoose.model('Flow', FlowSchema);

And the controllers code:

// servers/controllers/flows.js
'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Flow = mongoose.model('Flow'),
    _ = require('lodash');

/**
 * Find flow by id
 */
exports.flow = function(req, res, next, id) {
    Flow.load(id, function(err, flow) {
        if (err) return next(err);
        if (!flow) return next(new Error('Failed to load flow ' + id));
        req.flow = flow;
        next();
    });
};

/**
* New code count Flows
*/
exports.compte = function(req, res) {
    var c;
        flow.count({}, function(err, count) {
        if (err) return next(err);
        c = count;
        res.jsonp (count);
    });
};

/**
 * Show Flow
 */
exports.show = function(req, res) {
    res.jsonp(req.flow);
};

/**
 * List of Flows
 */
exports.all = function(req, res) {
    Flow.find().sort('-name').populate('name', 'application').exec(function(err, flows) {
        if (err) {
            res.render('error', {
                status: 500
            });
        } else {
            res.jsonp(flows);
        }
    });
};

I added also routes... But it doesn't work, do you think that I made some mistakes? thank you in advance for your help

user2981029
  • 584
  • 1
  • 5
  • 17
  • Express middleware always take three parameters: req, res, and next. Id in your flow method is 'undefined'. Most likely you will find it in req.params or req.body depending on how you implement routes. – Eugene Kostrikov Apr 22 '14 at 20:48

2 Answers2

3

The documentation shows you how: http://mongoosejs.com/docs/guide.html#collection

You can explicitly choose a collection when creating the Schema:

// Flow Schema
var FlowSchema = new Schema({
    /* attributes...*/
}, {
    collection: 'my-collection'
});

Or you can set it on the created schema later:

// Flow Schema
var FlowSchema = new Schema({
    /* attributes...*/
});

FlowSchema.set('collection', 'my-collection');

This was added in Mongoose 3.4 I believe, so check the verison of Mongoose you are using.

Lee Adams
  • 311
  • 1
  • 7
1

See this post for your query on enforcing a collection name:

Mongoose -- Force collection name

var mySchema = new Schema({
  foo: bar
}, { collection: qux });

where qux is your collection, assuming you connected to the correct db in your mongo-connect.

Community
  • 1
  • 1