我有两个模型Place和Tag。我在tag.js中创建了创建Tag()远程方法,我想在place.js中访问此函数。
tag.js
'use strict';
var request = require('request');
module.exports = function(Tag) {
Tag.crateaTag = function(name, callback) {
request({
method: 'POST',
url: 'http://localhost:3000/api/tags',
body:{
"name": name
},
json: true
},
function (error, response,body) {
if (error || response.statusCode != 200) {
console.log('Hiba' + error + ' \n' + response);
}
callback(error, body);
});
};
place.js
'use strict';
var request = require('request');
module.exports = function(Place) {
var app = require('../../server/server');
Place.createPlace = function(name, descreption, tagName, callback) {
var Tag = app.models.Tag; //???
request({
method: 'POST',
url: 'http://localhost:3000/api/tags',
body:{
"name": name,
"descreption": descreption,
"tag": Tag.createTag(tagName) //???
},
json: true
},
function (error, response, body) {
if (error || response.statusCode != 200) {
console.log('Hiba' + error + ' \n' + response);
}
callback(error, body);
});
};
};
我想发布标签,我希望在我的数据库中看到单独的集合。
在tag.js中将crateaTag
声明为远程方法:
Tag.remoteMethod('crateaTag', {
accepts: {
arg: 'name',
type: 'string',
required: true
},
description: "create a tag"
});
在place.js使用Tag
作为:
var Tag = app.models.Tag;
Tag.createTag('test', function(){
// do your stuff
});