【问题标题】:Sailsjs external module not working in serviceSailsjs 外部模块无法正常工作
【发布时间】:2016-11-03 20:51:05
【问题描述】:

我正在使用 restler (https://github.com/danwrong/restler) 从外部来源进行 api 调用。在 Sailsjs 中,据我了解,辅助函数称为服务。我将用于获取、发布等的 restler 代码放在他们自己的服务中,这样我就不会一遍又一遍地使用相同的代码重复自己。但是,在我的控制器中运行良好的 restler 函数不再在服务中运行。例如:

//api/services/myService.js
module.export{
        httpGet: function(){
        var rest = require('restler');
        rest.get('http://google.com').on('complete', function(result) {
        if (result instanceof Error) {
            console.log('Error:', result.message);
            this.retry(5000); // try again after 5 sec
        } else {
            console.log(result);
        }
        });

    }

}

我知道我的服务被正确使用;我尝试从服务中返回一个变量以进行仔细检查:

        httpGet: function(){
        var check = null;
        var rest = require('restler');
        rest.get('http://google.com').on('complete', function(result) {
        if (result instanceof Error) {
            check = false;
            console.log('Error:', result.message);
            this.retry(5000); // try again after 5 sec
        } else {
            console.log(result);
            check = true;
        }
        });
        return check;
        //in the controller,  myService.httpGet() returns null, not true or false
    }

任何帮助将不胜感激。 Salisjs v0.12.4

【问题讨论】:

    标签: rest express sails.js


    【解决方案1】:

    最好让服务接受回调。

    //api/services/myService.js
    module.exports = {
            httpGet: function(callback){
            var rest = require('restler');
            rest.get('http://google.com').on('complete', function(result) {
            if (result instanceof Error) {
                console.log('Error:', result.message);
                return callback(result, null)
                //this.retry(5000); // try again after 5 sec
            } else {
                console.log(result);
                return callback(null, result)
            }
            });
    
        }
    
    }
    

    然后在调用服务时从你的控制器传递一个回调

    myService.httpGet(function callback(err, result){
        // handle error 
    
       // use result
    
    })
    

    此外,关于您的问题,您将提前从服务中返回 return check;,并使用您分配给它的值 null

    PS:你可以使用 Promise 代替回调(callback hell

    【讨论】:

    • 一件小事。我认为应该有module.exports = {。否则语法错误。
    • @Bonanza 是的。确实。这是一个错字。 :)
    • 谢谢。我从根本上误解了回调;我试图在函数中返回一个值。
    【解决方案2】:

    您应该将httpGet 函数导出为模块对象的属性。基本上,你有一个“小”错字。而不是这个:

    module.export{
            httpGet: function(){
    

    你应该有这个:

    module.exports = {
            httpGet: function(){
    

    另外,如果你想返回结果,添加callback:

    module.exports = {
            httpGet: function(callback){
                     ... 
                     if (result instanceof Error) {
                        console.log('Error:', result.message);
                        return callback(result, null)
                     } else {
                        console.log(result);
                        return callback(null, result)
                     }
                        ...
    

    ...或者使用 Promises。

    【讨论】:

      猜你喜欢
      • 2019-07-05
      • 2021-11-06
      • 2020-05-19
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多