ReferenceError:在Object中分配无效的左侧

尝试完成OAuth2stream程,但不断获取未捕获的引用错误。 Node.js是相当新的东西,似乎无法找出发生了什么事情。

// require the blockspring package. var blockspring = require('blockspring'); var request = require('request'); // pass your function into blockspring.define. tells blockspring what function to run. blockspring.define(function(request, response) { // retrieve input parameters and assign to variables for convenience. var buffer_clientid = request.params["buffer_clientid"]; var buffer_secret = request.params["buffer_secret"]; var redirectURI = request.params["redirectURI"]; var tokencode = request.params["tokencode"]; request({ method: "POST", url: "https://api.bufferapp.com/1/oauth2/token.json", headers: { 'User-Agent': 'request', }, body: client_id=buffer_clientid&client_secret=buffer_secret&redirect_uri=redirectURI&code=tokencode&grant_type=authorization_code }, function(error, response, body){ console.log(body); // return the output. response.end(); }); }); 

这不是有效的JavaScript语法:

  body: client_id=buffer_clientid&client_secret=buffer_secret&redirect_uri=redirectURI&code=tokencode&grant_type=authorization_code 

我假设你正试图连接你的variables值到一个string? 试试这个:

  body: "client_id=" + buffer_clientid + "&client_secret=" + buffer_secret + "&redirect_uri=" + redirectURI + "&code=" + tokencode + "&grant_type=" +authorization_code 

需要引用nodej中的string。 在你的请求函数中,你传递了一个正文的关键字,其值是一个巨大的variables。 因为client_id=buffer_clientid&client_secret=buffer_secret&redirect_uri=redirectURI&code=tokencode&grant_type=authorization_code没有引号,它试图把它当作一个variables。 当parsing器到达=号时,它会尝试设置client_id =以下内容。 这是抛出错误。

只需引用整个string,或者如果您需要使用variablesconcat使用'string' + variable + 'string'

根据你的variables名来判断,你可以简单地重写它,如下所示:

 request({ method: "POST", url: "https://api.bufferapp.com/1/oauth2/token.json", headers: { 'User-Agent': 'request', }, body: 'client_id=' + buffer_clientid + '&client_secret=' + buffer_secret + '&redirect_uri=' + redirectURI + '&code=' + tokencode + '&grant_type=authorization_code' }, function(error, response, body){ console.log(body); // return the output. response.end(); })