These are chat archives for Automattic/mongoose

17th
Mar 2016
Mohit Sharma
@mohitsharmaniit11
Mar 17 2016 15:01
Fatal error: Schema hasn't been registered for model "tsp".
Use mongoose.model(name, schema)
Hi Guys I am getting this error

--------a.model.js--------
'use strict';

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

var A = new Schema({
name:String,
});

module.exports = mongoose.model('A', A);

--------b.model.js-------

'use strict';

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

var a = require('../a/a.model');

var B = new Schema({
aid: {type:Schema.Types.ObjectId, ref:"a"},
name:String
});

module.exports = mongoose.model('B', B);

--------queryfile.js---------

exports.all = function(req, res){
b.find().populate("aid").exec(function (err, b) {
if(err) {
console.log(err);
return handleError(res, err);
}
return res.status(200).json(b);
});

}

wfhio
@wfhio
Mar 17 2016 16:13
apologies, forget if i asked this in here already :)
what do you all do w/ _ids, do you use them in URLs etc?
Matthew Bonig
@mbonig
Mar 17 2016 16:20
@wfhio sure, why not?
wfhio
@wfhio
Mar 17 2016 16:27
just used to seeing more normal IDs in URLs, ie. starting from 0
so it's not taboo to do that then?
Matthew Bonig
@mbonig
Mar 17 2016 16:50
@wfhio Some would say it's not great, since it's turning your primary key into a natural key. The downside is that by not doing this you have to expose a lot of extra code to make your backend respond to natural keys instead of primary keys. I personally don't have a problem with see ObjectIDs in URLs.
wfhio
@wfhio
Mar 17 2016 16:55
thanks @mbonig!
LeonineKing1199
@LeonineKing1199
Mar 17 2016 20:29
Hey guys, if I use Model.save() in a Promise chain, what does Model.save() resolve to? The model?
Or does it resolve to an array of [model, numAffected]
I really wish the docs specified resolutions lol
Matthew Bonig
@mbonig
Mar 17 2016 22:15
@LeonineKing1199 http://mongoosejs.com/docs/api.html#model_Model-save, function(err, model,numAffected)
LeonineKing1199
@LeonineKing1199
Mar 17 2016 22:16
Right, but promises only resolve to one value. I'm going to go ahead and assume that it'll resolve only to the model and not the number affected or a single array or object containing both fields.
Matthew Bonig
@mbonig
Mar 17 2016 22:17
well, if you try it, what do you get?
LeonineKing1199
@LeonineKing1199
Mar 17 2016 22:18
It seems to resolve to the model but damn, that's such sketchy advice XD