当连接到neo4j时,aws lambda给出“无法反序列化请求:意外的输入结束:ARRAY的预期关闭标记”

时间:2016-12-06 07:44:35

标签: javascript amazon-web-services lambda neo4j cypher

我正在尝试连接到lambda函数中的neo4j数据库。但不断得到“无法反序列化请求:意外的输入结束:ARRAY的预期关闭标记”错误。但是当使用相同的参数在webapp中发送请求时,一切似乎都很好。这是我的要求机构:

  var request = {"statements": 
    [
        {
        "statement": "MATCH (p:COMPANY {id: " + event.ID + "})<-[:MADE_BY]-(FRANCHISE) RETURN FRANCHISE"
        }
    ]
  };
  var options = {
      host: hostname,
      path: pathname,
      method: 'POST',
      port: portnumber,
      headers: {
          "Authorization": authInfo,
          "Content-Type": "application/json",
          'Content-Length': Buffer.byteLength(request)
      },
  };
  console.log(JSON.stringify(request));
  var req = http.request(options, (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      res.setEncoding('utf8');
      res.on('data', (chunk) => {
          console.log(`BODY: ${chunk}`);
      });
      res.on('end', () => {
          console.log('No more data in response.');
      });
  });

  req.on('error', (e) => {
      console.log(`problem with request: ${e.message}`);
  });

  req.write(JSON.stringify(request));
  req.end();

还尝试打印JSON.stringify(request)结果,但对我来说似乎没问题。

1 个答案:

答案 0 :(得分:1)

当您调用Buffer.byteLength(request)时,您将传递request对象本身而不是该对象的JSON字符串形式。这会导致Content-Length标头值太小。

请改为尝试:

  var request = {"statements": [ {
        "statement": "MATCH (p:COMPANY {id: {event_id} })<-[:MADE_BY]-(FRANCHISE) RETURN FRANCHISE",
        "parameters": {
          "event_id": event.ID
        }
    } ]
  };
  var request_str = JSON.stringify(request);
  var options = {
      host: hostname,
      path: pathname,
      method: 'POST',
      port: portnumber,
      headers: {
          "Authorization": authInfo,
          "Content-Type": "application/json",
          'Content-Length': Buffer.byteLength(request_str)
      },
  };
  console.log(request_str);
  var req = http.request(options, (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      res.setEncoding('utf8');
      res.on('data', (chunk) => {
          console.log(`BODY: ${chunk}`);
      });
      res.on('end', () => {
          console.log('No more data in response.');
      });
  });

  req.on('error', (e) => {
      console.log(`problem with request: ${e.message}`);
  });

  req.write(request_str);
  req.end();

另请注意,此代码将event.ID作为参数传递,如果重复调用此代码,则效率会更高。