monguurl
monguurl copied to clipboard
Using on embedded documents
Getting the following error:
TypeError: undefined is not a function
at EmbeddedDocument.<anonymous> (/home/sharry/Development/Projects/site/node_modules/monguurl/lib/monguurl.js:40:12)
at _next (/home/sharry/Development/Projects/site/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:62:30)
Model
"use strict";
var mongoose = require('mongoose'),
monguurl = require('monguurl'),
timestamps = require('mongoose-timestamp');
var PageSchema = new mongoose.Schema({
title: {
type: String,
trim: true,
required: true
},
slug: {
type: String,
index: {
unique: true
}
},
body: {
type: String,
required: true
},
order: {
type: Number,
required: true
},
status: {
type: String,
required: true,
enum: ['live', 'hidden'],
default: 'hidden'
}
});
var SectionSchema = new mongoose.Schema({
title: {
type: String,
trim: true,
required: true
},
slug: {
type: String,
index: {
unique: true
}
},
excerpt: {
type: String,
required: true
},
order: {
type: Number,
required: true
},
pages: [PageSchema]
});
/**
* Book Schema
*/
var BookSchema = new mongoose.Schema({
contentType: {
type: String,
trim: true,
required: true
},
slug: {
type: String,
index: {
unique: true
}
},
section: [SectionSchema]
});
/////////////////////////////////////////////////////////////
// PLUGINS
/////////////////////////////////////////////////////////////
PageSchema.plugin(monguurl({
source: 'title',
target: 'slug'
}));
SectionSchema.plugin(monguurl({
source: 'title',
target: 'slug'
}));
BookSchema.plugin(monguurl({
source: 'contentType',
target: 'slug'
}));
Controller:
Book.findOne(
{
'slug': 'support'
}
)
.exec(function(err, book) {
if (err) {
return next(err);
} else {
var section = {
title: "Section 1",
excerpt: "Section 1 excerpt here",
order: 1
};
// Create a new section
book.section.push(section);
book.save(function (saveBookErr, book) {
if (saveBookErr) {
return next(saveBookErr);
} else {
res.send('Book updated')
}
});
}
});
Can't see anything obviously wrong in either your or my code. However i see that mongoose has released a major version since i wrote this plugin; could be something related to that (though I can't find anything relevant in their still shitty docs).
I'm currently on vacation so I'll be able to look into it more in two weeks. Meanwhile: what version of node and mongoose are you using? Any other plugins?
Thanks Andreas.
Node: v0.12.7 NPM: 2.12.0 Mongoose: ~4.0.1 Monguurl: ~0.1.0
Regarding plugins, only using yours & timestamps:
var timestamps = require('mongoose-timestamp');
BookSchema.plugin(timestamps, {
createdAt: 'createdDate',
updatedAt: 'modifiedDate'
});