“AxiosError:选项必须是对象:“错误错误选项值”

mm9b1k5b  于 2023-01-13  发布在  iOS
关注(0)|答案(1)|浏览(389)

我试图在axios get中传递一个数组作为参数。这是一个使用当前包的react应用程序。
我已经使用每个解决方案的代码来处理其他类似的问题。我已经尝试应用两种不同的方法/语法形式,从注解代码中可以看出。
下面是代码:

import Qs from "qs";
import axios from "axios";

console.log(
  "ContactContainer - useEffect[currentUser] - Before check of currentUser.contactRequestsTo.length > 0 ",
  currentUser.contactRequestsTo.length
);
const userIds = JSON.stringify(currentUser.contactRequestsTo);
//const userIds = currentUser.contactRequestsTo;
console.log("ContactContainer - useEffect[currentUser] - userIds ", userIds);
if (currentUser.contactRequestsTo.length > 0) {
  (async () => {
    //const contactRequestsToDetails = await axios.get(`${getUserDetailsRoute}`,{
    //  params: {
    //    arrayOfUserIds: userIds
    //  },
    //  paramsSerializer: (params) => Qs.stringify(params, {arrayFormat: 'repeat'})
    //});

    const instance = axios.create({
      paramsSerializer(params) {
        return Qs.stringify(params, { arrayFormat: "brackets" });
      },
    });

    const contactRequestsToDetails = await instance.get(getUserDetailsRoute);

    console.log(
      "ContactContainer - useEffect[currentUser] - contactRequestsToDetails = ",
      contactRequestsToDetails
    );
  })();
}

一些软件包版本如下:

"axios": "^1.2.1",
    "mongoose": "^6.8.0",
    "qs": "^6.11.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",

原始数组currentUser. contactRequestsTo为:

ContactContainer - useEffect[] - currentUser.contactRequestsTo =  
Array(2)
0:"63b476a79c42d2b60fc39cbc"
1:"63b476ea9c42d2b60fc39ccd"
length:2
[[Prototype]]:Array(0)

我尝试使用原始数组作为输入,然后对数组应用JSON.Stringify,以获得如下所示的UserId:

userIds  ["63b476a79c42d2b60fc39cbc","63b476ea9c42d2b60fc39ccd"]

我还使用了两种格式,一种是注解掉的,另一种如图所示。
在所有情况下,我收到了相同的错误。

Uncaught (in promise) 
code:"ERR_BAD_OPTION_VALUE"
message:"options must be an object"
name:"AxiosError"
stack:"AxiosError: options must be an object\n    at Object.assertOptions (http://localhost:8080/static/js/main.d2f30e44.js:2:356049)\

我看到其他用户也遇到了同样的错误,并在我解释解决方案时应用了更改。
我的语法是否有问题或者其他什么问题会导致这个错误?

bybem2ql

bybem2ql1#

paramsSerializer的配置在v1.0.0中已更改。它现在是一个具有encodeserializeindexes属性的对象。
如果你想在查询参数数组中添加方括号,可以使用下面的代码。另外,不要将数组序列化为JSON

(async () => {
  const currentUser = {
    contactRequestsTo: [
      "63b476a79c42d2b60fc39cbc",
      "63b476ea9c42d2b60fc39ccd"
    ],
  };
  const getUserDetailsRoute = "https://echo.zuplo.io/"; // just for the demo

  const contactRequestsToDetails = await axios.get(getUserDetailsRoute, {
    params: {
      arrayOfUserIds: currentUser.contactRequestsTo, // the actual array
    },
    paramsSerializer: {
      indexes: false, // empty brackets like `arrayOfUserIds[]`
    },
  });

  console.log(contactRequestsToDetails.data.url);
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.2.2/axios.min.js" integrity="sha512-QTnb9BQkG4fBYIt9JGvYmxPpd6TBeKp6lsUrtiVQsrJ9sb33Bn9s0wMQO9qVBFbPX3xHRAsBHvXlcsrnJjExjg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

请参见www.example.comhttps://github.com/axios/axios#request-config
由于indexes: false是默认值,因此您根本不清楚为什么您觉得需要使用paramsSerializer

相关问题