2017-10-23 2 views
0

Ich habe Schwierigkeiten, eine Dokumentation bezüglich der Umleitung auf eine URL innerhalb einer Modellfunktion oder remoteMethod zu finden. Hat schon jemand das hier gemacht? Bitte finden Sie meinen Code unten.Loopback/Express: Wie auf URL innerhalb einer remoteMethod umgeleitet werden?

Funktion innerhalb Model (Exposes/catch Endpunkt)

Form.catch = function (id, data, cb) { 
    Form.findById(id, function (err, form) { 

     if (form) { 
     form.formentries.create({"input": data}, 
      function(err, result) { 
      /* 
      Below i want the callback to redirect to a url 
      */ 
      cb(null, "http://google.be"); 
      }); 
     } else { 
     /* 
     console.log(err); 
     */ 
     let error = new Error(); 
     error.message = 'Form not found'; 
     error.statusCode = 404; 
     cb(error); 
     } 
    }); 
    }; 

    Form.remoteMethod('catch', { 
    http: {path: '/catch/:id', verb: 'post'}, 
    description: "Public endpoint to create form entries", 
    accepts: [ 
     {arg: 'id', type: 'string', http: {source: 'path'}}, 
     {arg: 'formData', type: 'object', http: {source: 'body'}}, 
    ], 
    returns: {arg: 'Result', type: 'object'} 
    }); 

Antwort

1

fand ich eine answer hier. Sie müssen ein remote hook erstellen und auf das Express-Objekt res zugreifen. Von dort können Sie res.redirect('some url') verwenden.

Form.afterRemote('catch', (context, remoteMethodOutput, next) => { 
    let res = context.res; 
    res.redirect('http://google.be'); 
}); 
+0

Dank für diese Antwort Denis, das funktioniert! – Jornve

0

Sie können Response-Objekt von HTTP-Kontext erhalten, ist es dann als Parameter in Remote-Methode injizieren, und es direkt verwenden:

Model.remoteMethodName = function (data, res, next) { 
    res.redirect('https://host.name.com/path?data=${data}') 
}; 

Model.remoteMethod('remoteMethodName', { 
    http: { 
     path: '/route', 
     verb: 'get', 
    }, 
    accepts: [ 
     {arg: 'data', type: 'string', required: false, http: {source: 'query'}}, 
     {arg: 'res', type: 'object', http: ctx => { return ctx.res; }}, 
    ], 
    returns: [ 
     {arg: 'result', type: 'any'} 
    ], 
}); 
Verwandte Themen