最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - LoopbackExpress: How to redirect to URL inside a remoteMethod? - Stack Overflow

programmeradmin0浏览0评论

I am having a hard time finding any documentation regarding redirecting to a URL inside a model function or remoteMethod. Has anyone here already done this? Please find my code below.

Function inside Model (Exposes /catch endpoint)

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

I am having a hard time finding any documentation regarding redirecting to a URL inside a model function or remoteMethod. Has anyone here already done this? Please find my code below.

Function inside Model (Exposes /catch endpoint)

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'}
    });
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 23, 2017 at 14:34 JornveJornve 3091 gold badge5 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

I found an answer here. You need to create a remote hook and access the res Express object. From there, you can use res.redirect('some url').

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

You can get response object from HTTP context, then inject it as a parameter into remote method, and use it directly:

Model.remoteMethodName = function (data, res, next) {
    res.redirect('https://host.name./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'}
    ],
});
发布评论

评论列表(0)

  1. 暂无评论