[go: nahoru, domu]

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

CRU fails for schema using Mongoose Map type #166

Closed
davad opened this issue Mar 1, 2019 · 2 comments
Closed

CRU fails for schema using Mongoose Map type #166

davad opened this issue Mar 1, 2019 · 2 comments

Comments

@davad
Copy link
Contributor
davad commented Mar 1, 2019

Describe the bug

When a schema has a Map field, create/read/update operations throw the following server-side error:

[09:41:56.302] 77020 LOG      api/test/Find — params({"_id":"5c7941a3ba4d188ef4d295f3"}), query({}), payload(null)
[09:41:56.309] 77020 ERROR    node_modules/rest-hapi/utilities/handler-helper.js:1693:9 api/test/Find — MongoError: Positional projection 'value.$*' does not match the query document.
[09:41:56.309] 77020 ERROR    node_modules/rest-hapi/utilities/handler-helper.js:1693:9 api/test/Find —     at queryCallback (/Users/david.m.landry/Sync/Avanade/MetLife/gssp-composer-backend/node_modules/mongodb-core/lib/cursor.js:248:25)
[09:41:56.309] 77020 ERROR    node_modules/rest-hapi/utilities/handler-helper.js:1693:9 api/test/Find —     at /Users/david.m.landry/Sync/Avanade/MetLife/gssp-composer-backend/node_modules/mongodb-core/lib/connection/pool.js:532:18
[09:41:56.309] 77020 ERROR    node_modules/rest-hapi/utilities/handler-helper.js:1693:9 api/test/Find —     at process._tickCallback (internal/process/next_tick.js:61:11)
Debug: internal, implementation, error
    Error: There was an error processing the request.
    at handleError (/Users/david.m.landry/Sync/Avanade/MetLife/gssp-composer-backend/node_modules/rest-hapi/utilities/handler-helper.js:1694:11)
    at Object._findHandler [as findHandler] (/Users/david.m.landry/Sync/Avanade/MetLife/gssp-composer-backend/node_modules/rest-hapi/utilities/handler-helper.js:321:5)

To Reproduce

Example schema:

module.exports = mongoose => {
    const modelName = "test";
    const Types = mongoose.Schema.Types;

    const Schema = new mongoose.Schema({
            value: {
                type: Map,
                of: Types.String,
            },
    });

    Schema.statics = {
        collectionName: modelName,
        routeOptions: {},
    };
    return Schema;
};

Expected behavior

Map types should be converted to/from plain objects. Joi would validate the shape of the contents of the Map, but not the Map keys themselves.

E.g. Sample JSON using the schema above; the first key/value, "key1", would pass Joi validation. The rest would fail.

{
    "value": {
        "key1": "a string",
        "key2": true,
        "key3": 5,
        "key4": ["a string"],
        "key5": {}
    }
}

This would pass as well:

{
    "value": {}
}

Desktop (please complete the following information):

  • OS: macOS
  • Browser cURL

Additional info

The DELETE operation still works. I created content directly in the DB, and was able to delete it through the REST endpoint.

@davad
Copy link
Contributor Author
davad commented Mar 1, 2019

I tracked down what's causing the error. I don't know if this is the only issue, but it allows me to create/read/update documents using a Map field.

Mongoose adds an extra entry to schema.paths for the Map type. It's called 'mapField.$*'. When that's included in the RestHapi queries, it throws the error mentioned above. When I filter out that path from the query, everything seems to work normally.

I'll submit a PR for that change.

@JKHeadley
Copy link
Owner

closed with #167

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