SailsJS:在提交的函数中获得SailsJS的Ajax形式的共鸣

Chr*_*ldt 1 mongodb ajaxform node.js sails.js

我想从ajax表单组件调用的操作中获取一个对象。

重现步骤。

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

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

  3. 在操作中,您将给定的数据安全到MongoDB并使用.fetch()提取创建的数据

  4. 您将获取的数据传递给 exits.success(fetchedData)

  5. 尝试在xxxx.page.js中的SubmittedForm函数中获取数据

我无法获取数据。我记录了ajax-form.components.js。在第398行,我们发出结果(结果应该具有我们的数据,在我的情况下是事实),但是之后结果消失了。也许我理解错了,显然我做错了。

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

str*_*eck 6

您在上述步骤中是正确的,并且我认为您所缺少的只是必须将Give参数添加到提交的函数中。作为vue模板中的道具,您传入($ event)。在页面脚本(page-name.page.js)中,您可以在定义提交函数的位置随意命名该参数。

尽管似乎您不需要它,但在这里我将举一个完整的示例,以防其他人在Sails.js中遇到Ajax形式的函数时遇到的麻烦。

在您的模板中(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">
Run Code Online (Sandbox Code Playgroud)

在这里,form-data将引用要存储数据的对象。密钥将来自您设置的v-model' as for a given input.规则is where you specify an object of objects. They key of each is the input name from。v-model and the value can be a string or array of strings for the rules set.form-errors specifies 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`指定一个对象,如果该操作返回非200响应,则后端错误将到达该对象。

在您的页面脚本(page-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);
    }
}
Run Code Online (Sandbox Code Playgroud)