2017-08-07 31 views
0

如何使用GRPC我回到用戶的列表,則結果爲空:如何將結果列表從sequelizejs返回給grpc?

http://condorjs.com,創建GRPC服務器

class User { 
    getUser(ctx) { 
    models.users.findAll({ 
      where: { userId: ctx.req.userId } 
     }).then(function(users){ 
      // return a list of users 
      for (var i = 0, len = users.length; i < len; i++) { 
      result.push({'userId': users[i].userId, 'userName': users[i].userName); 
      } 

      return { 'users': result}; 
     }); 
    } 
    } 

const options = { 
     'listen': '0.0.0.0:50051', 
     'rootProtoPath': 'protos', 
    }; 

    const app = new Condor(options) 
    .add('user.proto', 'User', new User()) 
    .start(); 

GRPC客戶電話:

var PROTO_PATH = __dirname + '/../proto/profile.proto'; 

var grpc = require('grpc'); 
var user_proto = grpc.load(PROTO_PATH).user; 

function main() { 
    var client = new guser_proto.User('localhost:50051', 
             grpc.credentials.createInsecure()); 

    client.getUser({userId: '8888'}, function(err, response) { 
     console.log(response); 
    }); 
} 

main(); 

的原型:

message UserResponse { 
    message User{ 
     string userId = 1; 
     string userName = 2; 
    } 

    repeated User users= 1; 
} 

回答

1

您還沒有收到任何cal在函數定義的lback方法,所以我添加了一個回調參數,這個回調方法將被調用所需的參數。你會在你的主要方法

getUser(ctx, callback) { 
    models.users.findAll({ 
      where: { userId: ctx.req.userId } 
     }).then(function(users){ 
      // return a list of users 
      for (var i = 0, len = users.length; i < len; i++) { 
      result.push({'userId': users[i].userId, 'userName': users[i].userName); 
      } 

      callback(null,{ 'users': result}); 
     }.bind(this)); 
    } 
    } 

該接收數據在這裏

function main() { 
    var client = new guser_proto.User('localhost:50051', 
             grpc.credentials.createInsecure()); 

    client.getUser({userId: '8888'}, function(err, response) { 
     console.log(response); // it will print here 
    }); 
} 
+0

是的,我已經試過了,它仍然是空一定會回報你的數據。 – Alvin

+0

你如何接受價值觀?其次,這個「用戶」對象是否有任何數據?您可以打印並檢查 –

+0

肯定的console.log給我的結果:[{用戶名: 'c46c4c9a0968450498f8de748d630c75'},{ 用戶名: '528ee92bbe9f4d3cbrc66fccf85cfb4c'},{ 用戶名: '3cd8004252454ae290e056999dec4ef7'}] – Alvin

相關問題