TypeError:无法读取未定义/ XML的属性“ setState”

时间:2019-05-24 09:49:09

标签: reactjs

我正在调用使用XML的服务,所以我想将XML解析为JSON,并将JSON数据集设置为我的反应状态,但是我正在获取。

function AuthenticateUser(Username,Password,Domain)
    dim strUser,strPass,strQuery,oConn,cmd,oRS
    AuthenticateUser = false
    strQuery = "SELECT cn FROM 'LDAP://" & Domain & "' WHERE 
objectClass='*'"
    set oConn = server.CreateObject("ADODB.Connection")
    oConn.Provider = "ADsDSOOBJECT"
    oConn.properties("User ID") = Username
    oConn.properties("Password")=Password
    oConn.properties("Encrypt Password") = true
    oConn.open "DS Query", Username,Password
    set cmd = server.CreateObject("ADODB.Command")
    set cmd.ActiveConnection = oConn
    cmd.CommandText = strQuery
    on error resume next
    set oRS = cmd.Execute
    if oRS.bof or oRS.eof then
        AuthenticateUser = false
    else
        AuthenticateUser = true
    end if
    set oRS = nothing
    set oConn = nothing
end function
 TypeError: Cannot read property 'setState' of undefined

1 个答案:

答案 0 :(得分:0)

您正在混合并匹配无法捕获function()和箭头(this)功能的=>

只需在各处使用箭头功能,即可正确捕获this(这是您的React组件):

axios
  .get(session_url)
  .then(response => {
    parseString(response.data, (err, result) => {
      if (err) {
        throw err;
      } else {
        this.setState({
          odm: result.data,
          loading: false,
        });
      }
    });
  })
  .catch(error => {
    console.log(error);
  });

如果可以的话,最好使用async函数:

// promisified version of `parseString`:
const parseStringP = data =>
  new Promise((resolve, reject) =>
    parseString(response.data, (err, result) => {
      if (err) return reject(err);
      resolve(result);
    }),
  );

// ...

try {
  const response = await axios.get(session_url);
  const result = await parseStringP(response.data);

  this.setState({
    odm: result.data,
    loading: false,
  });
} catch (error) {
  console.log(error);
}