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

feathers-mongoose@3.6.0 break custom model id #151

Closed
loicbisiere opened this issue Nov 14, 2016 · 4 comments
Closed

feathers-mongoose@3.6.0 break custom model id #151

loicbisiere opened this issue Nov 14, 2016 · 4 comments

Comments

@loicbisiere
Copy link

loicbisiere commented Nov 14, 2016

Steps to reproduce

Create a simple service with a custom model id :

"use strict";

const service = require("feathers-mongoose");
const orders = require("./orders-model");
const hooks = require("./hooks");

module.exports = function() {
  const app = this;

  const options = {
    Model: orders,
    id: "oId"
  };

  // Initialize our service with any options it requires
  app.use("/orders", service(options));

  // Get our initialize service to that we can bind hooks
  const ordersService = app.service("/orders");

  // Set up our before hooks
  ordersService.before(hooks.before);

  // Set up our after hooks
  ordersService.after(hooks.after);
};

orders-models.js :

"use strict";

// orders-model.js - A mongoose model
// 
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Mixed = Schema.Types.Mixed;

const ordersSchema = new Schema({
  oId: {
    type: String,
    required: true,
    trim: true,
    index: {
      unique: true
    }
  },
  amount: {
    type: Number,
    required: true,
    "default": 0
  },
  ...
  state: {
    type: String,
    "default": "new"
  }
});

const ordersModel = mongoose.model("orders", ordersSchema);

module.exports = ordersModel;

Create a "before" hook to initialize the "oId" on create :

exports.create = function() {
  return function(hook) {
    var randomId = Math.random().toString(36).substr(3, 2);
    hook.data.oId = "PO"+"-"+randomId+"-"+moment().unix();
    hook.data.state = "new";
  };
};

Expected behavior

Send a Post request to create a new "Order" without oId. The Order will be created with a oId generated by the hook.

Actual behavior

Mongoose validator send a missing error about the oId.

System configuration

Error happen since I updated feathers-mongoose@3.6.0.
No problem with feathers-mongoose@3.5.3

@daffl
Copy link
Member

daffl commented Nov 14, 2016

Can you post the order-model as well?

@daffl
Copy link
Member

daffl commented Nov 15, 2016

I bet the problem is 1222843#diff-480db08609ef6a51bf20f049cc9bda48R137. I'm not even sure why I added that...

@loicbisiere
Copy link
Author

I updated my first post with the orders-model.
Thank you.

@daffl
Copy link
Member

daffl commented Nov 15, 2016

This has been fixed in v3.6.1

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

2 participants