问题
So I've set up something really simple to learn how to use Loopback.
The models are as follows:
Person
- based on built in User model
food_pref
typeId (number)
personId (number)
food_type
type (string)
Relationships:
Person has many food_prefs (foreign key: personId)
food_pref belongs to Person (foreign key: personId)
food_pref belongs to food_type (foreign key: typeId)
An auto-generated method gets created that returns the food_prefs based on the id of the Person.
People/{id}/foodPrefs
This returns:
[
{
"typeId": 0,
"personId": 0,
"id": 0
}
]
What I want to do is add a separate remote method called "getPrefs" that returns the name of the type under food_type based on the typeId in food_pref.
So let's say typeId is 1 and id 1 in food_types is Italian Food then the remote method would return this:
{
"type": "Italian Food"
}
I was told to use Person.js and add something along these lines but I'm really confused about the include statement as well as what to do inside the brackets. Often it crashes with an error saying: Error: Relation "food_pref" is not defined for Person model, see what they recommended below:
module.exports = function(Person) {
Person.getPrefs = function(personId, cb) {
Person.findById(personId, { include: { food_pref: "food_type" } }, function(err, user) {
if (err) throw err;
});
}
Person.remoteMethod (
'getPrefs',
{
http: {path: '/getPrefs', verb: 'get'},
accepts: {arg: 'personId', type: 'number', http: { source: 'query' } },
returns: {arg: 'type', type: 'string'}
}
);
};
What am I doing wrong here?
回答1:
Edit:
According to strongloop documentation when you define a personal remote method, strongloop automatically provides a callback that will returns datas if needed. See below updated code
You want to include the food_pref relation as well as the food_type realation inside this food_pref. Get this into your getPrefs custom method:
Person.getPrefs = function(personId, cb) {
Person.findById(personId, {
include: [{
relation: 'food_pref',
scope: {
include: {
relation: 'food_type'
}}}
]},
function(err, personFound) {
if (err)
console.log(err);
else {
cb(null, personFound)
}
});
};
What it does: your personal method is called with the personId argument and a cb argument (automatically passed by strongloop !). Your method finds the right person by id, includes the relations (name of the type of food as well), then when the result has been fetched, the callback inside "Person.findById" calls the callback 'cb' with datas fetched (here personFound)
Person.remoteMethod(
'getPrefs', {
http: {path: '/:personId/getPrefs', verb: 'get'},
accepts: [{arg: 'personId', type: 'number'}],
returns: {arg: 'person', type: 'object'},
description: ['a person object']
}
);
Then the returned object should contain the name of the type of food. Make sure you include the right relation names you have in your Person.json and so on.
If you just want the name of food pref, follow the same idea with different methods:
just send the string inside your object to the automatic callback argument 'cb' (and modify the registration to announce the kind of return your method sends)
search directly inside food_type table with a "where" condition (where personId = the id of the person you are looking for)
Take a look at the link at the link to strongloop doc as it is pretty and detailed regarding remote methods.
Hope it helped.
来源:https://stackoverflow.com/questions/31254864/how-do-i-use-a-remote-method-in-one-model-to-return-info-from-another-model