代码之家  ›  专栏  ›  技术社区  ›  chuckieDub

访问帆动作中的URL参数

  •  1
  • chuckieDub  · 技术社区  · 7 年前

    我有一个帆应用程序,我正在尝试加载一个页面,其中包含一些数据库中的数据,基于 id recipe 模型。

    我有路线 'GET /recipes/single-recipe/:id': { action: 'recipes/view-single-recipe' } 设置并访问URL http://localhost:1337/recipes/single-recipe/5b8c169936f1df3439fa39c7

    在我的 view-single-recipe 操作我试图通过访问URL参数的ID加载配方 req.param('id') 但是 req 显示未定义。

    //view-single-recipe.js
    module.exports = {
    
    
      friendlyName: 'View single recipe',
    
    
      description: 'Display "Single recipe" page.',
    
    
      exits: {
    
        success: {
          viewTemplatePath: 'pages/recipes/single-recipe'
        }
    
      },
    
    
      fn: async function(inputs, exits) {
        const recipe = await Recipe.find({
          id: req.param('id') //req undefiend
        });
        // Respond with view.
        return exits.success({
          recipe: recipe
        });
    
      }
    
    
    };

    获取错误: error: Sending 500 ("Server Error") response: ReferenceError: req is not defined

    如何使用url参数加载正确的配方?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Dženis H.    7 年前

    /recipes/single-recipe/:id' id req.params.id {} info

    Sails.js machine-as-action route-handling example more information

    request this.req req req is not defined this