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

时间:2018-08-25 13:49:41

标签: node.js axios cloudant

我正尝试使用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)});

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

  

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

出了什么问题?

2 个答案:

答案 0 :(得分:3)

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

应该是

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

答案 1 :(得分:2)

您将url放在config confie参数中,但它必须在config之前。

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