环回,在同一模型中声明2个远程方法,但只能在api资源管理器中使用一个

问题描述:

我试图让2个远程方法在同一个环回模型中工作,但只有一个工作在api资源管理器中。如果我评论/删除其中一个的代码,另一个完美地工作。下面是我如何实现它:环回,在同一模型中声明2个远程方法,但只能在api资源管理器中使用一个

module.exports = function(Usrs) { 

    var db = server.dataSources.wifiMongo; 

    db.connect(function(err, db) { 
     Usrs.authMethod = function(cb) { 
      db.collection('users', function(err, collection) { 
       if (err) 
        return console.log('Error al encontrar la collección, err = ', err); 

       collection.aggregate([{ 
        "$group": { 
         _id: "$strategy", 
         count: { 
          $sum: 1 
         } 
        } 
       }, { 
        $project: { 
         tmp: { 
          label: '$_id', 
          value: '$count' 
         } 
        } 
       }, { 
        $group: { 
         _id: 'methods', 
         data: { 
          $addToSet: '$tmp' 
         } 
        } 
       }], function(err, methods) { 
        if (err) { 
         console.log('err = ' + err); 
        } else { 
         methods[0].data.forEach(function(method) { 
          method.color = randomColor({ 
           luminosity: 'bright' 
          }); 
         }); 
         cb(null, methods[0].data); 

        } 
       }); 
      }); 
     }; 
    }); 

    Usrs.remoteMethod('authMethod', { 
     http: { 
      path: '/analytics/authMethod', 
      verb: 'get' 
     }, 
     returns: { 
      arg: 'authMethod', 
      type: 'Object' 
     } 
    }); 

    //-------------------------------------------------------------------------// 

    db.connect(function(err, db) { 
     if (err) return console.log('error al conectar wifiMongo, err = ', err); 

     Usrs.devices = function(cb) { 
      db.collection('clients', function(err, collection) { 
       if (err) 
        return console.log('Error al encontrar la collección, err = ', err); 
       collection.aggregate([{ 
        "$group": { 
         _id: "$os", 
         count: { 
          $sum: 1 
         } 
        } 
       }], function(err, client) { 
        if (err) { 
         console.log(err); 
        } else { 
         var output = client.reduce(function(a, b) { 
          var match = b._id ? b._id.match(/(Mac|Windows|Android|iOS)/) : null; 
          match = match ? match[0] : 'Others'; 
          a[match] = (a[match] || 0) + b.count; 
          return a; 
         }, {}); 

         output = Object.keys(output).map(function(k) { 
          return { 
           label: k, 
           value: output[k], 
           color: randomColor({ 
            luminosity: 'bright' 
           }) 
          }; 
         }); 

         cb(null, output); 

        } 
       }); 
      }); 

     }; 

    }); 
    Usrs.remoteMethod('devices', { 
     http: { 
      path: '/analytics/devices', 
      verb: 'get' 
     }, 
     returns: { 
      arg: 'devices', 
      type: 'Object' 
     } 
    }); 


}; 

如果在api资源管理器中尝试它,第一个运行没有错误。 enter image description here 另一个给我内部服务器错误。 enter image description here 的记录以下:

Unhandled error for request GET /usrs/analytics/devices: TypeError: Cannot read property 'collection' of undefined 
    at Function.Usrs.devices (/home/ubuntu/loopback/common/models/usrs.js:72:6) 
    at SharedMethod.invoke (/home/ubuntu/loopback/node_modules/strong-remoting/lib/shared-method.js:262:25) 
    at HttpContext.invoke (/home/ubuntu/loopback/node_modules/strong-remoting/lib/http-context.js:295:12) 
    at phaseInvoke (/home/ubuntu/loopback/node_modules/strong-remoting/lib/remote-objects.js:647:9) 
    at runHandler (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:135:5) 
    at iterate (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13) 
    at Object.async.eachSeries (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:162:9) 
    at runHandlers (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:144:13) 
    at iterate (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13) 
    at /home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:157:25 
    at /home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25 
    at execStack (/home/ubuntu/loopback/node_modules/strong-remoting/lib/remote-objects.js:492:7) 
    at RemoteObjects.execHooks (/home/ubuntu/loopback/node_modules/strong-remoting/lib/remote-objects.js:496:10) 
    at phaseBeforeInvoke (/home/ubuntu/loopback/node_modules/strong-remoting/lib/remote-objects.js:643:10) 
    at runHandler (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:135:5) 
    at iterate (/home/ubuntu/loopback/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13) 

我甚至已经试过将它们放置在不同的车型,但仍然出现同样的错误。

始终处理错误。

连接到wifiMongo时可能会出错,一旦检查。

db.connect(function(err, db) { 
    if (err){ console.log('error al conectar wifiMongo, err = ', err); 
     return cb(err); 
    } 

    Usrs.devices = function(cb) {