TypeError:无法读取未定义的属性“方法”

时间:2019-02-19 21:47:00

标签: javascript node.js rpc

当尝试为我们获得的方法运行RPC命令时,我似乎无法弄清楚问题所在。它在本地但不在实时Linux服务器上工作。

  

TypeError:无法读取未定义的属性“方法”,位于   C:\ ssc-exchange-tranactions \ app.js:23:13位于Layer.handle [as   handle_request]   (C:\ ssc-exchange-transactions \ node_modules \ express \ lib \ router \ layer.js:95:5)   在下一个   (C:\ ssc-exchange-transactions \ node_modules \ express \ lib \ router \ route.js:137:13)在Route.dispatch   (C:\ ssc-exchange-transactions \ node_modules \ express \ lib \ router \ route.js:112:3)   在Layer.handle [作为handle_request]   (C:\ ssc-exchange-transactions \ node_modules \ express \ lib \ router \ layer.js:95:5)   在   C:\ ssc-exchange-transactions \ node_modules \ express \ lib \ router \ index.js:281:22   在Function.process_params   接下来(C:\ ssc-exchange-transactions \ node_modules \ express \ lib \ router \ index.js:335:12)   (C:\ ssc-exchange-transactions \ node_modules \ express \ lib \ router \ index.js:275:10)在jsonParser   (C:\ ssc-exchange-transactions \ node_modules \ body-parser \ lib \ types \ json.js:119:7)   在Layer.handle [作为handle_request]   (C:\ ssc-exchange-transactions \ node_modules \ express \ lib \ router \ layer.js:95:5)

const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const port = 5000;
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const dsteem = require('dsteem');
const client = new dsteem.Client('https://api.site.com');
app.set('trust proxy', true);
app.disable('x-powered-by');


app.post('/', function(req,res){
    try
    {
    var body=JSON.parse(req.body.curl);
    }
    catch(err)
    {
     res.send({msg: 'invalid command', error: err});
    }
    if(body.method !=undefined && body.method == "POST")
    {
    let options = {
      url:body.url,
      method:body.method,
      headers:body.headers,
      body: JSON.stringify(body.body)
    };
    request(options, (error, response, body) => {
      if (error) {
        console.error("An error has occurred: ", error);
        res.send(error);
      }
      else {
        let responseData = JSON.parse(body);
        res.send(responseData);
      }
    });
  }
  ///Ends if
  else if(body.method != undefined && body.method == "GET")
  {
    let options = {
    //   url: body.url+'?account='+body.body.params.account+'&limit='+body.body.params.limit+
    //   '&offset='+body.body.params.offset+'&&symbol='+body.body.params.symbol,
         url: 'https://api.site.com/accounts/history'+body.symbol,
         method: "GET",
         headers: {"Content-type": "application/json"},
    };
    request(options, (error, response, body) => {
      if (error) {
        console.error("An error has occurred: ", error);
        res.send(error);
      }
      else {
         var withdraw = [], deposit = [];
         body= JSON.parse(body);
         body.forEach(el => {
           if(el.from == "account"){
            delete el.block;
            delete el.symbol;
            delete el.from_type;
            delete el.to_type;
             withdraw.push(el);
           }
           else{
            delete el.block;
            delete el.symbol;
            delete el.from_type;
            delete el.to_type;
            deposit.push(el);
           }

         });
         res.json([{"WITHDRAWS": withdraw},{"DEPOSITS":deposit}]);
      }
    });
  }
  //ends else if
  else
  {
    const active_key = body.wif;
    const key = dsteem.PrivateKey.fromString(active_key);
    const account = "account";
    const my_id= "mainnet";
    const my_data= {"contractName":"tokens", "contractAction":"transfer","contractPayload":{"symbol": "omg",
    "to": body.to,"quantity":body.quantity,"memo": body.memo }};
    client.broadcast.json({
        required_auths: [account],
        required_posting_auths: [],
        id: my_id,
        json: JSON.stringify(my_data),
    }, key).then(
        result => {res.send(result)},
        error => {res.send({msg: 'Something went wrong', error: error})}
    )
  }
  //ends else
  });

app.listen(port, function () {
  console.log("Server listening on port: " + port);
});

1 个答案:

答案 0 :(得分:0)

如果您的第一个try / catch块中出现错误,您的代码就会中断,这是您的语义错误。

try
{
  var body=JSON.parse(req.body.curl);
}
catch(err)
{
   res.send({msg: 'invalid command', error: err});
}

1。 JS中的变量提升

检查js中的变量提升主题:https://developer.mozilla.org/en-US/docs/Glossary/Hoisting

简而言之,根据您的示例,这意味着JS编译器将在函数的最顶部的 post 函数内创建一个名为body的变量,该变量的初始值为undefined。发生这种情况是因为您使用了var关键字,而不是const / let。

仅在JSON.parse()工作正常的情况下,将设置body变量的值。如果发生这种情况(看起来像是您所遇到的那样)或发生错误,body将永久保持未定义状态,这意味着您无法访问其属性(例如method),因为它不是对象。

解决方案取决于您要在这里实现的目标:

  1. 您也可以将其余代码也放入try / catch
  2. 您还可以为身体添加其他检查
  3. 您可以对代码进行一些重构以使其更具可读性(这当然总是主观的,并且取决于编码样式)

此处重构的示例:

app.post('/', function(req, res) {
  try {
    const body = JSON.parse(req.body.curl);

    switch (body.method) {
      case 'GET':
        this.handleGETRequest();
      case 'POST':
        this.handlePOSTRequest();
      default:
        this.handleDefault();
    }
  } catch (err) {
    res.send({ msg: 'invalid command', error: err });
  }
});

如果要避免拆分为多个功能,可以执行类似的操作

app.post('/', function(req, res) {
      try {
        const body = JSON.parse(req.body.curl);

        // Your current else 
        if(!body.method) {
            ...code
            return; // Will break the execution after this
        }

        ...your normal if/else here with simplified checks now
      } catch (err) {
        res.send({ msg: 'invalid command', error: err });
      }
    });

考虑到一切都包裹在try / catch中,因此无需专门检查此处未定义的主体。