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

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

我正在尝试在 index.html 中的 onclick 事件上调用 enrollUser 函数。 enrollUser() 也在 index.js 中定义。它仍然给我 onclick 事件的错误。

这里有代码供您参考。让我看看这些有什么问题..

enrollUser.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

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

<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

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');
            });
};

也在 index.html 中给 app.js 报错--> GET http://localhost:3000/app.js net::ERR_ABORTED --> 404 (Not Found)

在控制台上没有收到任何错误,但它在 chrome 开发者工具上。请让我知道缺少什么。

我正在使用与该项目相同的结构https://github.com/avi-githb/Certification_Network_Hyperledger_fabric/tree/master/application

要按照您在评论中解释的方式进行这项工作,您需要先 运行 服务器。对于您来说,这是 app.js 文件。

确保安装了 node 并且 运行 在你的项目目录中使用这个命令。

node index.js

您将通过在控制台中看到带有端口的消息来了解服务器 运行ning。

其次,修改您的 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>

这应该适合您,了解有关表单的更多信息 here