2017-10-23 4 views
0

モデル関数内のURL、またはremoteMethodへのリダイレクトに関するドキュメントを見つけるのに苦労しています。誰もここで既にこれをしていますか?以下の私のコードを見つけてください。ループバック/エクスプレス:remoteMethod内のURLにリダイレクトする方法は?

モデル内の機能(公開する/キャッチエンドポイント)

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'} 
    }); 

答えて

1

私はここanswerを見つけました。 remote hookを作成し、res Expressオブジェクトにアクセスする必要があります。そこからres.redirect('some url')を使用できます。

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

おかげで、これは動作しています! – Jornve

0

あなたは、リモート・メソッドへのパラメータとして、それを注入し、その後、HTTPコンテキストからレスポンスオブジェクトを取得し、それを直接使用することができます。この回答デニスのための

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'} 
    ], 
}); 
関連する問題