NODEJS-AXIOS:错误:“ url”参数必须为字符串类型。在Url.parse处收到的类型对象

MET*_*EAD 4 node.js cloudant axios

我正在尝试使用AXIOS从Rest API获取数据,如下所示:

require('dotenv').config();
const axios = require('axios');

var url = 'https://931eb067-05c0-492a-8129-48ebfc27d426-bluemix.cloudant.com/dummy/_design/NEW_VIEW_DESIGN/_view/new-view?include_docs=true';
axios.get({url,auth: {
    username: process.env.account,
    password: process.env.password 
}}).then((res)=>{console.log(res.data);})
.catch((e)=>{console.log(e)});
Run Code Online (Sandbox Code Playgroud)

我可以通过提供凭据来单独访问提及的URL,但是在使用AXIOS时出现以下错误

“ url”参数必须为字符串类型。在Url.parse处收到的类型对象

出了什么问题?

Chr*_*ins 7

axios.get({url,auth: {
    username: process.env.account,
    password: process.env.password
}}).then((res)=>{console.log(res.data);})
Run Code Online (Sandbox Code Playgroud)

应该

axios.get(url,auth: {
    username: process.env.account,
    password: process.env.password
}).then((res)=>{console.log(res.data);})
Run Code Online (Sandbox Code Playgroud)


mcs*_*sym 6

你已经把urlconfig confie 参数放在里面,但它必须在 config.conf 之前。

axios.get(url, {auth: {
    username: process.env.account,
    password: process.env.password
}}).then((res)=>{console.log(res.data);})
.catch((e)=>{console.log(e)});
Run Code Online (Sandbox Code Playgroud)