These are chat archives for Automattic/mongoose

19th
Dec 2015
Anubhav Agarwal
@TheGelato
Dec 19 2015 07:43
can we get collections without using any schema or model?
Anubhav Agarwal
@TheGelato
Dec 19 2015 08:28
Also what is the use of defining model name other than it uses plural for collection name?
Riccardo Amadio
@rokity
Dec 19 2015 13:04
Hi everyone
'''text
/**
 * User constructor
 * @constructor
 */
function User() {
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    var userSchema = new Schema({
        name: String,
        username: { type: String, required: true, unique: true },
        password: { type: String, required: true },
        admin: Boolean,
        location: String,
        meta: {
            age: Number,
            website: String
        },
        created_at: Date,
        updated_at: Date
    });
    module.exports=mongoose.model('User', userSchema);
}
this is my User.js file
and this is my roue (main code ) where call to save User on collection
*route
 var User = require('./User.js');


// create a new user called chris
    var chris = new User({
        name: 'Chris',
        username: 'sevilayha',
        password: 'password'
    });
    chris.save(function(err) {
        if (err) throw err;

        console.log('User saved successfully!');
    });
in run time
it crash when I did chris.save
Riccardo Amadio
@rokity
Dec 19 2015 13:09
I'm sorry
the error is User is not a Fuction
User is not a function
Riccardo Amadio
@rokity
Dec 19 2015 13:30
I fixed this problem
and now I have
OverwriteModelError: Cannot overwrite Downloads model once compiled.
Downloads is my model
dontwork
@dontwork
Dec 19 2015 19:00
How would you set up having 2 mongoose Schemas, but one must have a property which is an array of instances of the other
and then what for instance if you wish to use properties from that array in a virtual you want to create
Riccardo Amadio
@rokity
Dec 19 2015 19:05
server.js
var Downloads=require('./Downloads')(MongoDB);
             var d=new Downloads({
             infoHash:torrent.infoHash,
             nome:torrent.name,
             content:torrent.files,
             down_speed:0,
             progress:0,
             tot_down:0,
             status:'started'
             });
             d.save(function(err){
             console.log("Added Torrent");
             });
Downloads.js
/**
 * Downloads Schema
 * @param url
 * @returns {Model}
 */
module.exports=function(url) {
        /**Initialize Connection **/
    var mongoose = require('mongoose');
    var db = mongoose.createConnection(url);
    /**Declare Fields of Collection **/
    var DownloadsSchema = new mongoose.Schema({
            infoHash:String,
            nome:String,
            content:[],
            down_speed:Number,
            progress:Number,
            tot_down:Number,
            status:String
    });

    /** Define Model **/
    var Downloads = db.model('Downloads', DownloadsSchema);
    return Downloads;
}
why I get this error
RangeError: Maximum call stack size exceeded
why ???