如何创建回环外部CRUD? [英] How to create loopback external CRUD?

查看:202
本文介绍了如何创建回环外部CRUD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用回送是一个中间人,以两种不同的API(服务器1和服务器)之间的连接。当我通过用户的电子邮件这个中间人,它会检查与服务器1,如果该服务器返回true,那么中间人会从服务器2的所有数据保存到服务器1。

I want to use loopback to be a "middle man" to connect between two different api (server1 and server2). When I pass a user's email to this "middle man", it will check with server 1, and if that server returns true, then the "middle man" will get all data from the server 2 save to server 1.

有人能指导我如何做到这一点?我试图按照环回文件,但我无法得到我想要的,我缺少的东西吗?

Can someone guide me on how to do this? I tried to follow loopback documentation, but I couldn't get what I want, am I missing something?

推荐答案

假设你已经建立了两个数据源调用外部API提供商按我的答案<一个href=\"http://stackoverflow.com/questions/34131073/loopback-connector-rest-api/34153753#34153753\">your相关的问题 - 在这个例子中,你需要以下呼叫:

Assuming you have set up two datasources that call your external API providers as per my answer to your related question - for this example you would need the following calls:

Server1的数据源:

Server1 datasource:


  • checkEmail // GET方法,返回true或false

  • 保存 // POST方法,返回true或false

  • checkEmail // GET method, returns true or false
  • save // POST method, returns true or false

Server2的数据源:

Server2 datasource:


  • getDetails // GET或POST方法,返回详细联系方式的电子邮件地址

  • getDetails // GET or POST method, returns contact details for the email address

您还需要为您的入口点的适当方法的环回模式。对于这个例子可以调用它接触,名为商店远程方法,以在电子邮件参数。这code会做所有的工作:

You would also need a Loopback Model with an appropriate method as your entry point. For the example lets call it Contact, with a remote method called store, taking in the email parameter. This code would do all of the work:

Contact.store = function(email, cb) {
    var server1 = Contact.app.dataSources.Server1;
    var server2 = Contact.app.dataSources.Server2;

    server1.checkEmail(email, function(validated) {
        if(validated) {
            server2.getDetails(email, function(details) {
                if(details) {
                    server1.save(details, function(success) {
                        if(success) {
                            cb(null, details);
                        }
                    })
                }
            })
        }

    })
}

(你可以promisify上述所有的清洁code)

(you can promisify all of the above for cleaner code)

这篇关于如何创建回环外部CRUD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆