无法重构流星 HTTP 请求函数
Can't refactor a meteor HTTP request function
我是 meteor/js 的新手,如果这是一个愚蠢的问题,我真的很抱歉。
旧函数是这样的:
import {HTTP} from 'meteor/http';
export function AccountLogin(data, url) {
console.log('test header');
// console.log(header);
HTTP.post(url, {
headers: {
'Content-Type': 'application/json',
'Client_id': 'test',
'Client_secret': '1234'
},
npmRequestOptions: {
rejectUnauthorized: false,
strictSSL: false,
timeout: 1000
},
data: JSON.stringify(data)
}, function (error, response) {
if (error) {
console.log(error);
} else {
console.log(response);
console.log(response.statusCode);
console.log(response.content);
}
});
}
然后这样调用:
import {Meteor} from 'meteor/meteor';
import {AccountLogin} from "../import/AccountCaller";
Meteor.startup(() => {
// code to run on server at startup
const data = {
"username": "test",
"password": "test"
};
console.log(data.username);
let url = 'https://localhost:5001/api/account/authenticate';
const headers = {
'Content-Type': 'application/json',
'Client_id': 'test',
'Client_secret': '1234'
};
AccountLogin(data, url);
});
我想将 header 移动到参数中,所以我这样做:
export function AccountLogin(data, url, header) {
HTTP.post(url, {
header,
//... the rest
并修改main中的调用,
AccountLogin(data, url ,header);
将 header 移动到参数时,服务器响应不同。我想知道重构这个函数时我做错了什么?
看起来像是一个简单的错字:
HTTP.post(url, {
headers: header, // or rename your 3rd argument as "headers" (notice the trailing "s")
npmRequestOptions,
data
}, callback);
我是 meteor/js 的新手,如果这是一个愚蠢的问题,我真的很抱歉。
旧函数是这样的:
import {HTTP} from 'meteor/http';
export function AccountLogin(data, url) {
console.log('test header');
// console.log(header);
HTTP.post(url, {
headers: {
'Content-Type': 'application/json',
'Client_id': 'test',
'Client_secret': '1234'
},
npmRequestOptions: {
rejectUnauthorized: false,
strictSSL: false,
timeout: 1000
},
data: JSON.stringify(data)
}, function (error, response) {
if (error) {
console.log(error);
} else {
console.log(response);
console.log(response.statusCode);
console.log(response.content);
}
});
}
然后这样调用:
import {Meteor} from 'meteor/meteor';
import {AccountLogin} from "../import/AccountCaller";
Meteor.startup(() => {
// code to run on server at startup
const data = {
"username": "test",
"password": "test"
};
console.log(data.username);
let url = 'https://localhost:5001/api/account/authenticate';
const headers = {
'Content-Type': 'application/json',
'Client_id': 'test',
'Client_secret': '1234'
};
AccountLogin(data, url);
});
我想将 header 移动到参数中,所以我这样做:
export function AccountLogin(data, url, header) {
HTTP.post(url, {
header,
//... the rest
并修改main中的调用,
AccountLogin(data, url ,header);
将 header 移动到参数时,服务器响应不同。我想知道重构这个函数时我做错了什么?
看起来像是一个简单的错字:
HTTP.post(url, {
headers: header, // or rename your 3rd argument as "headers" (notice the trailing "s")
npmRequestOptions,
data
}, callback);