【问题标题】:Object Not Passing/Fetch-Post Request/API/NodeJS/Browser对象未通过/Fetch-Post 请求/API/NodeJS/浏览器
【发布时间】:2020-12-25 21:24:34
【问题描述】:

问题:在 JavaScript 中通过从 HTML 中的注册表单输入数据创建的对象似乎没有被创建。

试过了:

  1. 我检查了我是否使用 {data} 引用了整个对象,而只是引用了数据
  2. 搜索其他在线资源无济于事
  3. 再次阅读 JavaScript 对象,看看我是否犯了一个简单的错误
  4. 添加调试字符串给我提示(我将在下面列出代码)

相关代码

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() {}

错误代码

浏览器:

  1. 除了signupData(form) 函数中的console.log(JSON.stringify({data})); 之外,它还会运行signup.html 文件中的所有内容。
  2. 可疑,因为使用用户数据创建的对象应该已创建并打印到控制台。

我的 API 控制台:

  1. 我不会引用 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


    【解决方案1】:

    您是否在使用 NodeJ 和 express 之类的框架?如果是,那么您必须确保您的后端能够传递传入的 JSON 请求。如果你使用 express,你可以只使用 express.json() 中间件。确保你把它放在其他中间件的顶部或者像这样的cors中间件之后

    app.use(express.json())
    

    这将解析来自前端的传入数据。

    【讨论】:

    • 感谢您的建议。我会尝试这个,但是,对象在发送到 api 后不会打印到控制台,这让我相信该对象只是返回未定义的客户端。你认为这有什么解释吗?
    • 是的,看起来我已经启用了它
    • app.use(bodyParser.json());
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2019-11-11
    • 2021-08-02
    • 1970-01-01
    • 2017-05-04
    • 2020-10-18
    相关资源
    最近更新 更多