0

我目前正在通过 fetch in expo 提出这个请求

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);
  }
}
4

1 回答 1

0

让我们定义你自己的格式化字符串函数并移动你的try catchif 表达式

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=${1}', id, token))

于 2017-04-27T09:06:06.057 回答