Sails JS:在提交的函数中获取 Sails JS Ajax-Form 的响应

SailsJS: Get Resonse of Ajax-Form of SailsJS in the submitted function

我想从 ajax 表单组件调用的操作中获取 object。

重现步骤。

  1. 创建一个 ajax 表单,您可以在其中传递模型的值(例如 post 的标题和描述)

  2. 处理提交表单后,将数据传递给操作

  3. 在操作中,您将把给定的数据安全保存到 MongoDB 并使用 .fetch()

  4. 获取创建的数据
  5. 你把抓取的数据传给exits.success(fetchedData)

  6. 尝试获取xxxx.page.js

  7. 中submittedForm函数中的数据

我无法获取数据。我记录了 ajax-form.components.js。在第 398 行,我们发出结果(结果应该有我们的数据,在我的例子中它是事实)但在那之后,结果就消失了。 可能是我理解错了,明明是我做错了

如果您需要更多信息,请告诉我。

你在上面描述的步骤中是正确的,我认为你所缺少的只是你必须给你提交的函数提供参数。作为vue模板中的一个prop,你传入($event)。在页面脚本 (page-name.page.js) 中,您可以在定义提交函数的位置随意命名参数。

虽然您似乎不需要它,但我将在此处提供一个详尽的示例,以防其他人在使用 Sails.js 中的 ajax-form 函数时遇到问题。

在您的模板中(html):

<ajax-form
    action="<camelcase of the file for your action>" 
    :handle-parsing="parseForm"
    :submitted="submittedForm($event)"
    @rejected="rejectedForm($event)"
    :form-data="formData"
    :form-rules="formRules"
    :form-errors.sync="formErrors"
    :cloud-error.sync="cloudError"
>
<input type="text" id="input1" v-model="input1">

此处,form-data 将引用存储数据的对象。密钥将来自您设置的 v-model' as for a given input.form-rulesis where you specify an object of objects. They key of each is the input name fromv-modeland the value can be a string or array of strings for the rules set.form-errorsspecifies an object where errors will go if the form triggers any errors upon submission. If this happens, data does not get sent to the server and neither the submitted or rejected function get run.cloud-error.sync` 指定一个对象,其中如果操作 returns 非 200 响应,任何后端错误都会消失。

在您的页面脚本中(页面-name.page.js):

data: {
    formData: {},
    formErrors: {},
    formRules: {
        input1: 'required'
    },
    cloudError: ''
},
methods: {
    parseForm: function () {
        // You can do parsing and custom validations here, but return all data 
        // you want to send to the server as an object called 'argins'
        return argins;
    },
    submittedForm (data) {
        // Here you can use any data that is returned from the action, like
        console.log('returned data: ', data);
    },
    rejectedForm (err) {
        // This function runs if the server returns a non-200 response
        console.log(err);
    }
}