【发布时间】:2020-12-25 21:24:34
【问题描述】:
问题:在 JavaScript 中通过从 HTML 中的注册表单输入数据创建的对象似乎没有被创建。
试过了:
- 我检查了我是否使用 {data} 引用了整个对象,而只是引用了数据
- 搜索其他在线资源无济于事
- 再次阅读 JavaScript 对象,看看我是否犯了一个简单的错误
- 添加调试字符串给我提示(我将在下面列出代码)
相关代码:
signup.html(每个sn-p都是从上到下的顺序):
<form id="signup-form" name ="signup-form">
<input class="login-form-field" type="text" name="user" placeholder="username">
<input class="login-form-field" type="text" name="email" placeholder="email">
<input class="login-form-field" type="password" name="dob" placeholder="date of birth">
<br>
<!--<button class="actionButton"></button>-->
<INPUT TYPE="button" NAME="button" Value="Click" onClick="signupData(this.form)">
</form>
//last of the markup body with Browserify compiled JavaScript files linked for functionality
<script src="browserify/builds/genKey.js"></script>
<script src="browserify/builds/SignUp.js"></script>
<script LANGUAGE="JavaScript">
function signupData(form) // add to this script
{
console.log("signup data is starting");
var user = form.user.value;
var email = form.email.value;
var dob = form.dob.value;
genSKey();
genPKey();
var skey = getSKey();
// var enUser = encryptMes(user);
// var enEmail = encryptMes(email);
var endob = encryptMes(dob);
var data = { name: "LifeNet", members: { user: {profilePic: {}, endob, listeners: {}, listening: {}, friends: {}, requested: {}, blocked:{}, channel: false} } }
apiPost({data});
// pass the signup function in here
// hash the variables and send to celox network
console.log(JSON.stringify({data}));
alert (`copy and save your Private Key to somewhere safe: ${skey}`);
}
</script>
signup.js(浏览器前构建):
window.apiPost = function({data})
{
fetch("https://goldengates.club:3000/api/passData",
{
method: "post",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({data})
}
);
}
build.js(浏览器前构建):
var eccrypto = require("eccrypto");
window.genSKey = function()
{
var secretKey = eccrypto.generatePrivate();
var SKey = JSON.stringify(secretKey);
localStorage.setItem("skey", SKey);
console.log(SKey);
alert(`your private key is ${SKey}`)
}
window.genPKey = function()
{
var skey = localStorage.getItem("skey");
var SKey = JSON.parse(skey);
let publicKey;
if(SKey != null)
{
publicKey = eccrypto.getPublic(SKey);
localStorage.setItem("pkey", JSON.stringify(publicKey));
return;
}
publicKey = eccrypto.getPublic(privateKey);
localStorage.setItem("pkey", JSON.stringify(publicKey));
return;
}
window.getPKey = function()
{
var PKey = localStorage.getItem("pkey");
var pkey = JSON.parse(PKey);
return pkey;
}
window.getSKey = function()
{
var SKey = localStorage.getItem("skey");
var skey = JSON.parse(SKey);
return skey;
}
window.encryptMes = function(data)
{
//for this you need to get the sender's public key to encrypt the message
if (localStorage.getItem("pkey") === null)
{
if (localStorage.getItem("skey") === null)
{
genSKey();
genPKey();
}
}
var pkey = getPKey();
encryptedMes = eccrypto.encrypt(pkey, Buffer.from(data));
return encryptedMes;
}
window.decryptMes = function(data)
{
if (localStorage.getItem("skey") === null)
{
genSKey();
}
var skey = getSKey();
decryptedMes = eccrypto.decrypt(SKey, data);
return decryptedMes.toString();
}
window.encryptData = function()
{
genSKey();
genPKey();
enMes = encryptedMes(/*add a document search for all fields on input form in login*/);
}
window.decryptData = function() {}
错误代码:
浏览器:
- 除了
signupData(form)函数中的console.log(JSON.stringify({data}));之外,它还会运行signup.html 文件中的所有内容。 - 可疑,因为使用用户数据创建的对象应该已创建并打印到控制台。
我的 API 控制台:
-
我不会引用 API 代码,因为在我看来,对象只是没有被发布到它,而且这不是问题。
TypeError: Cannot read property 'name' of undefined
at dataPool.setData (/home/main/public_html/Cypher-Network/src/app/data-Pool.js:64:27)
at /home/main/public_html/Cypher-Network/src/index.js:198:12
at Layer.handle [as handle_request] (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/layer.js:95:5)
at next (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/layer.js:95:5)
at /home/main/public_html/Cypher-Network/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/index.js:335:12)
at next (/home/main/public_html/Cypher-Network/node_modules/express/lib/router/index.js:275:10)
at /home/main/public_html/Cypher-Network/src/index.js:62:3
非常感谢任何形式的帮助和解释,因为我对 JavaScript 的工作方式非常陌生。
【问题讨论】:
标签: javascript node.js json fetch browserify