Giter Club home page Giter Club logo

Comments (5)

jedireza avatar jedireza commented on May 18, 2024

Yeah we need that callback to be sent through the resultFactory. Would you be willing to create a gist that reproduces the error so I can help debug?

from hapi-mongo-models.

edimoldovan avatar edimoldovan commented on May 18, 2024

Yes, of course, I’ll try to put that together today or tomorrow. Thx for the reply!

from hapi-mongo-models.

edimoldovan avatar edimoldovan commented on May 18, 2024

I prepared a repo instead, with two example calls which reproduce the issue: https://github.com/edimoldovan/hapi-mongo-models-example

If you do this:

curl -X POST -H "Content-Type: application/json; charset=utf-8" "127.0.0.1:8000/api/users" -d '{"email":"[email protected]","password":"bla","name":"name”}’

a user will be inserted, but not returned in the response. Also, if you hit this url:

http://localhost:8000/api/users

you will see that the data array contains the correct number of objects, only they are empty.

If I do the change I mentioned above, all this suddenly becomes ok.

from hapi-mongo-models.

jedireza avatar jedireza commented on May 18, 2024

Thanks so much. That made it much easier to help figure out why the objects were empty for you.

Your model was missing a constructor. When we create a new instance of a model we pass the instance data into it's constructor.

Here is what I did to fix your example.

$ npm install object-assign --save

Then in your model I added a constructor:

var Joi = require('joi');
var BaseModel = require('hapi-mongo-models').BaseModel;
var ObjectAssign = require('object-assign');

var User = BaseModel.extend({
    constructor: function (attrs) {

        ObjectAssign(this, attrs);
    }
});

User._collection = 'users'; // the mongo collection name

User.schema = Joi.object().keys({
    name: Joi.string(),
    image: Joi.string(),
    password: Joi.string().required(),
    email: Joi.string().email().required()
});

module.exports = User;

Then both GET and POST worked as expected.

from hapi-mongo-models.

edimoldovan avatar edimoldovan commented on May 18, 2024

Works very well, thank you for your help. The readme update would also be welcome, thate is where I copied my code from. Suppose will be done later.

from hapi-mongo-models.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.