简体   繁体   English

如何修复:TypeError:enrollUser is not a function at HTMLButtonElement.onclick

[英]How to Fix : TypeError: enrollUser is not a function at HTMLButtonElement.onclick

I am trying to call enrollUser function on onclick event in index.html.我正在尝试在 index.html 中的 onclick 事件上调用注册用户 function。 enrollUser() is also defined in index.js.在 index.js 中也定义了enrollUser()。 Still it giving me an error on onclick event.它仍然给我一个关于 onclick 事件的错误。

Heres are codes for your reference.以下是供您参考的代码。 Let me what's wrong in these..让我看看这些有什么问题。。

enrollUser.js注册用户.js

'use strict';
const FabricCAServices = require('fabric-ca-client');
const { Wallets } = require('fabric-network');
const fs = require('fs');
const yaml = require('js-yaml');
const path = require('path');

async function main(uname) {
    try {
        // load the network configuration
        let connectionProfile = yaml.safeLoad(fs.readFileSync('../gateway/connection-uni.yaml', 'utf8'));

        // Create a new CA client for interacting with the CA.
        const caInfo = connectionProfile.certificateAuthorities['ca.uni.example.com'];
        const caTLSCACerts = caInfo.tlsCACerts.pem;
        const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);

        console.log(`----------------- Creating ID for: ${uname} -----------------`)
        
        //function body

}

module.exports.execute = main;

index.js index.js

const express = require('express');
const app = express();
const cors = require('cors');
const port = 3000;


const enrollUser = require('./enrollUser');

app.use(cors());
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.set('title', 'Educhain App');

app.get('/', function (req, res) {  
   res.sendFile( __dirname + "/" + "index.html" );  
});


app.post('/enrollUser', (req, res) => {
    enrollUser.execute(req.body.uname)
            .then(() => {
                console.log('User credentials added to wallet');
                const result = {
                    status: 'success',
                    message: 'User credentials added to wallet',
                    uname: req.body.uname
                };
                res.json(result);
            })
            .catch((e) => {
                const result = {
                    status: 'error',
                    message: 'Failed',
                    error: e
                };
                res.status(500).send(result);
            });
});

app.listen(port, () => console.log(`Distributed Certification App listening on port ${port}!`));

index.html索引.html

<script type="text/javascript" src="./app.js"></script>
<input type="text" placeholder="Enter Username" name="uname" id="uname" required>
<button type="submit" id="enrollUser" onclick="enrollUser()">Login</button></div>

app.js应用程序.js

let enrollUser = () => {
    const uname = document.getElementById('uname').value;
    
    $.post('http://localhost:3000/enrollUser', {uname: uname})
            .done((result) => {
                console.log(result);
                if (result.status === 'success') {
                    $(".studentTable tbody").append(
                            "<tr>" +
                            "<td>1</td>" +
                            "<td id='uname'>" + result.uname + "</td>" +
                            "</tr>"
                    );
                } else {
                    $(".error-toast").toast('show');
                }
            })
            .fail((xhr, status, error) => {
                $(".error-toast").toast('show');
            });
};

Also giving error to app.js in index.html--> GET http://localhost:3000/app.js net::ERR_ABORTED -- > 404 (Not Found)还给 index.html 中的 app.js 出错--> GET http://localhost:3000/app.js net::ERR_ABORTED --> 404 (Not Found)

Not getting any error on console, but it's on chrome developer tool.在控制台上没有收到任何错误,但它在 chrome 开发人员工具上。 Please let me know what's missing.请让我知道缺少什么。

I am using the same structure as this project https://github.com/avi-githb/Certification_Network_Hyperledger_fabric/tree/master/application我使用与这个项目相同的结构https://github.com/avi-githb/Certification_Network_Hyperledger_fabric/tree/master/application

To make this work the way you explained in the comments you would need to run the server first.要按照您在评论中解释的方式进行这项工作,您需要先运行服务器。 For you, this is the app.js file.对你来说,这是app.js文件。

Make sure to have node installed and run this command in your project directory.确保已安装节点并在项目目录中运行此命令。

node index.js

You will know the server running by seeing a message with the port in the console.您将通过在控制台中看到带有端口的消息来了解正在运行的服务器。

Secondly, modify your HTML to contain a form, then pass the form on submitting to the endpoint enrollUser其次,修改您的 HTML 以包含一个表单,然后将表单提交到端点enrollUser

<form action="http://localhost:3000/enrollUser" method="POST">
<input type="text" placeholder="Enter Username" name="uname" id="uname" required>
<button type="submit">Login</button>
</form>

This should work well for you, learn more about forms here .这应该很适合您, 在此处了解有关 forms 的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTMLButtonElement.onclick中未定义如何修复功能 - How to fix function is not defined at HTMLButtonElement.onclick 未捕获的类型错误:btnSubmit 不是 HTMLButtonElement.onclick 处的 function - Uncaught TypeError: btnSubmit is not a function at HTMLButtonElement.onclick TypeError:Functionname 不是 HTMLButtonElement.onClick 中的函数 - TypeError: Functionname is not a function at HTMLButtonElement.onClick 未捕获的TypeError:链接不是HTMLButtonElement.onclick中的函数 - Uncaught TypeError: links is not a function at HTMLButtonElement.onclick 未捕获的类型错误:deleteButton 不是 HTMLButtonElement.onclick 上的函数 - Uncaught TypeError: deleteButton is not a function at HTMLButtonElement.onclick 类型错误:“类型错误:函数名不是 HTMLButtonElement.onclick (/:2:54) 处的函数” - TypeError: "TypeError: function name is not a function at HTMLButtonElement.onclick (/:2:54)" Javascirpt:未捕获的TypeError:对象不是函数HTMLButtonElement.onclick - Javascirpt: Uncaught TypeError: object is not a function HTMLButtonElement.onclick 函数未在 HTMLButtonElement.onclick 中定义 - function is not defined at HTMLButtonElement.onclick 函数未在 HTMLButtonElement.onclick 中定义 - Function not defined at HTMLButtonElement.onclick 未捕获的TypeError:this.openA不是HTMLButtonElement.onclick上的函数 - Uncaught TypeError: this.openA is not a function at HTMLButtonElement.onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM