sailsJs - 从控制器传递的数据在视图中不可用

sailsJs - data passed from controller is not available in view

我已经为这个问题苦苦挣扎了几个小时,我找不到解决方案,我在互联网上也找不到解决方案,我一遍又一遍地阅读文档,我是按照我所知道的那样进行操作,但它不起作用。

我正在尝试将一个数组从控制器传递到视图,但它无法正常工作,即使我使用 if 语句,我也会收到变量未定义的错误,即使我使用 locals.errors(错误是我的数组从控制器发送),它仍然不会显示任何数据。

控制器

/**
 * AuthControllerController
 *
 * @description :: Server-side actions for handling incoming requests.
 * @help        :: See https://sailsjs.com/docs/concepts/actions
 */

module.exports = {
  
    login: async function (req, res) {

    },

    signup: async function (req, res) {

        let username = req.param('username'),//regex condition should be added to model
            password = req.param('password'),
            confirmedpass = req.param('confirmedpass'),
            email = req.param('useremail');

        let errors = [];

        if (password !== confirmedpass) {
            errors.push('Passwords doesn\'t match');
        }

        if (!_.isString(username) || username.length > 20 || !username.match(/^[a-zA-Z0-9]+$/)) {
            errors.push('Username must not be longer as 20 characters, letters and numbers only');
        }

        if (await Account.findOne({username: username})) {
            errors.push('Username already exists');
        }

        if (await Account.findOne({email: email})) {
            errors.push('Email is already in use');
        }

        if (errors.length > 0) {
            console.log(`Errors ${errors}`);

            return res.view('pages/signup', {errors: errors, layout: 'layouts/blanklayout'});
        }

        try {
            await Account.create({username: username, password: password, email: email});
        }
        catch(e) {
            return console.log(e.message);
        }

        return console.log(`Username: ${username} | Password: ${password} | Email: ${email}`);
    },

    logout: async function (req, res) {

    },

    loadView: function (req, res) {

        return res.view('pages/signup', {layout: 'layouts/blanklayout'});

    }

};

这是视图布局

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="/images/favicon.ico">

    <title>LC-RP - Dashboard</title>
    
    <link rel="stylesheet" href="/styles/vendors_css.css">
    <link rel="stylesheet" href="/styles/style.css">
    <link rel="stylesheet" href="/styles/skin_color.css">

    <link href="https://fonts.googleapis.com/icon?family=Material+Icons"
      rel="stylesheet">
</head>

<body class="hold-transition theme-primary bg-img" style="background:
radial-gradient(circle at 100% 50%, transparent 20%, rgba(255,255,255,.2) 21%, rgba(255,255,255,.2) 34%, transparent 35%, transparent),
radial-gradient(circle at 0% 50%, transparent 20%, rgba(255,255,255,.2) 21%, rgba(255,255,255,.2) 34%, transparent 35%, transparent) 0 -50px;
background-color: rgb(154, 160, 165);
background-size:75px 100px;" data-overlay="5">
      
        <%- body %>
    
    
    <script src="/js/vendors.min.js"></script>
    <script src="/dependencies/assets/icons/feather-icons/feather.min.js"></script>
    <script src="/js/pages/toastr.js"></script>
    <% if ( errors ) { %> 
    <script>
      $(function() {
        let testVar = "<%- errors %>";

        console.log(`test, ${locals}`);
      })
    </script>
    <% } %> 
</body>
</html>

您可以看到我尝试在脚本标记中显示视图布局代码底部的错误。

这是在布局中,但即使我在视图本身中这样做,它也不起作用,错误不显示任何数据,它甚至不存在。

控制器必须 return 到 assets/js/pages/auth.page.js(Vue2+ 的包装器),后者又为 views/pages/auth.ejs 提供变量。

您似乎只在此处发送回复:if (errors.length > 0)。因此,您可以在 finally 块中的 try-catch 之后移动 return res.view()


        try {
            await Account.create({username: username, password: password, email: email});
        }
        catch(e) {
            errors.push(e.message);
        } finally {
            return res.view('pages/signup', {errors: errors, layout: 'layouts/blanklayout'});
        }

或者在布局中包含if (typeof errors === ‘undefined’ {var errors = []}