Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem trying to post a record to a nested collection #10

Closed
simax opened this issue Jul 7, 2013 · 18 comments
Closed

Problem trying to post a record to a nested collection #10

simax opened this issue Jul 7, 2013 · 18 comments

Comments

@simax
Copy link

simax commented Jul 7, 2013

I'm having a problem trying to post a record to a nested collection

This is my schema:

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

var EmployeeSchema = new Schema({
    firstname: {
        type: String,
        required: true,
        trim: true
    },
    lastname: {
        type: String,
        required: true,
        trim: true
    },
    email: {
        type: String,
        required: true,
        trim: true,
        index: true,
        validate: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/
    },
    departmentId: {
        type: objectId,
        trim: true,
        required: true
    },
    enddate: {
        type: String,
        trim: true
    },
    active: {
        type: Boolean,
        "default": true
    }
});

var DepartmentSchema = new Schema({
    name: {
        type: String,
        required: true,
        trim: true,
        index: {
            unique: true
        }
    },
    employees: [EmployeeSchema]
});

module.exports.employee =  mongoose.model('employee', EmployeeSchema);
module.exports.department  = mongoose.model('department', DepartmentSchema);

This is my server startup code :

var express = require('express'),
    mers = require('mers');
    require('./Schemas/DataSchemas.js');

    process.env.NODE_ENV = 'development';

    var server = express();

    server.configure(function() {

        server.use(express.bodyParser());
        server.use('/app', express.static('app'));

        server.use(express.errorHandler({ dumpExceptions: true, showStack: true }));

        server.use('/api', mers({ uri:'mongodb://localhost:8120/staff' }).rest());
    });

    server.configure('development', function(){
        server.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    });

    server.configure('production', function(){
        server.use(express.errorHandler());
    });

    server.listen(3000);

When I attempt to post to the following URL:

http://localhost:3000/api/department/1234/employees

I receive a 404. What am I doing wrong?

@jspears
Copy link
Owner

jspears commented Jul 8, 2013

That is more than likely a bug. Should have a fix in a day or two.

@simax
Copy link
Author

simax commented Jul 8, 2013

Cool, thanks for the prompt response.

@simax
Copy link
Author

simax commented Jul 27, 2013

It looks like you started work on fixing this bug (and thought you might have it complete in a couple of days), but that was 3 weeks ago. Is this problem proving too difficult or have you just not had to time to work on it? MERS looks really promising but IMHO it really needs to handle sub-docs.

@jspears
Copy link
Owner

jspears commented Jul 27, 2013

Sorry just busy, ill put aside some time Sunday to fix.

Sent from my iPhone

On Jul 27, 2013, at 6:49 AM, Simon Lomax notifications@github.com wrote:

It looks like you started work on fixing this bug (and thought you might
have it complete in a couple of days), but that was 3 weeks ago. Is this
problem proving too difficult or have you just not had to time to work on
it? MERS looks really promising but IMHO it really needs to handle
sub-docs.


Reply to this email directly or view it on
GitHubhttps://github.com//issues/10#issuecomment-21663646
.

@simax
Copy link
Author

simax commented Jul 27, 2013

Excellent, thanks for your great work we really appreciate it.

@jspears
Copy link
Owner

jspears commented Aug 5, 2013

Sorry for not getting this fixed, there are so many cases... to check for. I am still working on it.

@CyberCM
Copy link

CyberCM commented Aug 5, 2013

+1. Lucky I found this post too... Was really breaking my head. Mers looks amazing, so thumbs up!

@simax
Copy link
Author

simax commented Aug 6, 2013

Looking forward to the results of your efforts, should be cool.

@meertens
Copy link

Running into the same problem. Was this working in a previous version?

@jspears
Copy link
Owner

jspears commented Aug 16, 2013

no it was never implemented. The implementation needs to do a lot of
stuff to work in all the situations so its taking a bit more time to
implement than I actually have. Be cool if someone wanted to take a stab
at fixing it, I can give some direction.

On Fri, Aug 16, 2013 at 10:49 AM, meertens notifications@github.com wrote:

Running into the same problem. Was this working in a previous version?


Reply to this email directly or view it on GitHubhttps://github.com//issues/10#issuecomment-22770700
.

@meertens
Copy link

Sorry, I won't have time (or the skills) for this any time soon.

@jspears
Copy link
Owner

jspears commented Sep 9, 2013

I think its fixed. I am sure there are corner cases it doesn't handle but check 0.7.0 and let me know if you have issues. Thanks for the reports.

@jspears jspears closed this as completed Sep 9, 2013
@jspears
Copy link
Owner

jspears commented Sep 9, 2013

Shoot that was just put not post... ok. reopening hopefully it won't take long.

@jspears jspears reopened this Sep 9, 2013
@jspears
Copy link
Owner

jspears commented Sep 9, 2013

Alright, still need to implement delete, but post put are working so, calling it done for today.

@jspears jspears closed this as completed Sep 9, 2013
@simax
Copy link
Author

simax commented Sep 16, 2013

Just upgraded to version 0.7.1-0. Using the example above I can now POST to url: http://localhost:3000/api/department/1234/employees and a new employee is created. Its added to the employees collection, nested within a department document. Excellent !

However if I then try to PUT to http://localhost:3000/api/department/1234/employees/321 (assuming 321 is the id of the employee just created) I get an error stating "Cannot set property 'firstname' of undefined." Where firstname is part of the employee model.

Am I doing something wrong.

@jspears
Copy link
Owner

jspears commented Sep 16, 2013

Currently, it just uses the positional index, not the ID, that would be a
good feature to have. I will see about adding it in the next couple of
days.
http://localhost:3000/api/department/1234/employees/http://localhost:3000/api/department/1234/employees/3210
should work.

On Mon, Sep 16, 2013 at 10:28 AM, Simon Lomax notifications@github.comwrote:

Just upgraded to version 0.7.1-0. Using the example above I can now POST
to url: http://localhost:3000/api/department/1234/employees and a new
employee is created. Its added to the employees collection, nested within a
department document. Excellent !

However if I then try to PUT to
http://localhost:3000/api/department/1234/employees/321 (assuming 321 is
the id of the employee just created) I get an error stating "Cannot set
property 'firstname' of undefined." Where firstname is part of the employee
model.

Am I doing something wrong.


Reply to this email directly or view it on GitHubhttps://github.com//issues/10#issuecomment-24513835
.

@simax
Copy link
Author

simax commented Sep 16, 2013

Cool, same for delete would be great as well.

@jspears
Copy link
Owner

jspears commented Sep 16, 2013

will add that to the list of stuff. Wed, I usually get to work on
things, so look for a new release thurs.

On Mon, Sep 16, 2013 at 10:44 AM, Simon Lomax notifications@github.comwrote:

Cool, same for delete would be great as well.


Reply to this email directly or view it on GitHubhttps://github.com//issues/10#issuecomment-24515067
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants