我如何将变量添加到 javascript 中的 url 请求,更具体地说,expo 和 react native
how do i add variables to url requests in javascript, more specifically, expo and react native
我目前正在 expo
通过 fetch 提出这个请求
const response = await fetch(`https://graph.facebook.com/v2.9/{{{i need to enter the id here}}}/friends?access_token=${token}`);
我有 user.id 但我不确定如何在字符串中调用 id
这里是完整的函数,除了字符串中的 {id} 之外其他都有效
login = async () => {
const ADD_ID = '<APP ID>'
const options = {
permissions: ['public_profile', 'email', 'user_friends'],
}
const {type, token} = await Expo.Facebook.logInWithReadPermissionsAsync(ADD_ID, options)
if (type === 'success') {
const response = await fetch(`https://graph.facebook.com/me?access_token=${token}`)
const user = (await response.json());
const id = user.id;
console.log(user);
console.log(id);
this.authenticate(token)
}
try {
const response = await fetch(`https://graph.facebook.com/v2.9/{id}/friends?access_token=${token}`);
console.log(await response.json())
} catch(error) {
console.error(error);
}
}
让我们定义您自己的格式字符串函数,并将您的 try catch
移入 if 表达式
String.format = function() {
// The string containing the format items (e.g. "{0}")
// will and always has to be the first argument.
var theString = arguments[0];
// start with the second argument (i = 1)
for (var i = 1; i < arguments.length; i++) {
// "gm" = RegEx options for Global search (more than one instance)
// and for Multiline search
var regEx = new RegExp("\{" + (i - 1) + "\}", "gm");
theString = theString.replace(regEx, arguments[i]);
}
return theString;
}
console.log(String.format('https://graph.facebook.com/v2.9/{0}/friends?access_token=', id, token))
我目前正在 expo
通过 fetch 提出这个请求const response = await fetch(`https://graph.facebook.com/v2.9/{{{i need to enter the id here}}}/friends?access_token=${token}`);
我有 user.id 但我不确定如何在字符串中调用 id
这里是完整的函数,除了字符串中的 {id} 之外其他都有效
login = async () => {
const ADD_ID = '<APP ID>'
const options = {
permissions: ['public_profile', 'email', 'user_friends'],
}
const {type, token} = await Expo.Facebook.logInWithReadPermissionsAsync(ADD_ID, options)
if (type === 'success') {
const response = await fetch(`https://graph.facebook.com/me?access_token=${token}`)
const user = (await response.json());
const id = user.id;
console.log(user);
console.log(id);
this.authenticate(token)
}
try {
const response = await fetch(`https://graph.facebook.com/v2.9/{id}/friends?access_token=${token}`);
console.log(await response.json())
} catch(error) {
console.error(error);
}
}
让我们定义您自己的格式字符串函数,并将您的 try catch
移入 if 表达式
String.format = function() {
// The string containing the format items (e.g. "{0}")
// will and always has to be the first argument.
var theString = arguments[0];
// start with the second argument (i = 1)
for (var i = 1; i < arguments.length; i++) {
// "gm" = RegEx options for Global search (more than one instance)
// and for Multiline search
var regEx = new RegExp("\{" + (i - 1) + "\}", "gm");
theString = theString.replace(regEx, arguments[i]);
}
return theString;
}
console.log(String.format('https://graph.facebook.com/v2.9/{0}/friends?access_token=', id, token))