ray smith
ray smith

Reputation: 380

Loopback remote method not returning response body

So I created this remote method in loopback:

Message.findUserMessages = function(id,cb) {

        Message.find({ 
            where: { 
                from_user_id: id 
            },
            include: {
                "relation":"message_text"
            }

        });
    };

    Message.remoteMethod('findUserMessages', {
        accepts: {
            arg: 'id',
            type: 'number'
        },
        returns: {
            arg: 'response',
            type: 'Object'
        },
        http: {
            path: '/user/',
            verb: 'get'
        }
    });

But when I view the response, it does not show the output in the response body. The only reason I know the correct results are being accessed is due to the fact that my DB is returning the result of the query. How do I get put the output of the query in the response body?

Upvotes: 0

Views: 884

Answers (1)

xangy
xangy

Reputation: 1205

The correct code should be:

Message.findUserMessages = function(id, cb) {
    Message.find({ 
        where: { 
            from_user_id: id 
        },
        include: {
            "relation":"message_text"
        }
    }, function(err, response) {
        if (err) throw err;
        cb(null, response);
    });
};

Message.remoteMethod('findUserMessages', {
    accepts: {
        arg: 'id',
        type: 'number',
        required: true,
        http: { source: 'path' }
    },
    returns: {
        arg: 'response',
        type: 'Object',
        root: true
    },
    http: {
        path: '/user/:id/findUserMessages',
        verb: 'get'
    }
});

You forget to callback the response.

Note: I've also changed the http url path hoping you wanted it like so. And also source to the argument is set to path. You might also want to look at usage of root.

Upvotes: 1

Related Questions