引用错误:对象赋值中的左侧无效

ReferenceError: Invalid left-hand side in assignment at Object

本文关键字:无效 赋值 错误 对象 引用      更新时间:2023-09-26

试图完成OAuth2流,但不断收到未捕获的引用错误。对 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

我假设您正在尝试将变量值连接到字符串?试试这个:

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

nodejs 中的字符串需要用引号括起来。在你的请求函数中,你传递了一个 body 键,其值似乎是一个巨大的变量。因为client_id=buffer_clientid&client_secret=buffer_secret&redirect_uri=redirectURI&code=tokencode&grant_type=authorization_code周围没有引号,所以它试图将其视为一个变量。当解析器到达=符号时,它会尝试设置 client_id = 以下内容。这是抛出错误。

只需引用整个字符串,或者如果您需要使用变量 concat 使用 'string' + variable + 'string' .

从您的变量名称来看,您可以简单地重写如下:

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