0

Per Pushbullet API doc I need: Header as: 'Access-Token: o.I'veLearnedNotToPost' 'Content-Type": "application/json' Url is https://api.pushbullet.com/v2/users/me

Problem with my code (don't laugh too hard) is that it only returns "Retrieving information for o.I'veLearnedNotToPost"

Front end code needs to take token from user input text element and put it in backend code (getuser.jsw) to fetch User Info. But the issue is that it's not - nothing except the placeholder text appears in the textbox element (id #result)

Side note: $w is Wix's write to element

// FRONT END CODE
import {getUserInfo} from 'backend/getuser';

export function button1_click(event, $w) {
    console.log("Retrieving information for " + $w("#sendToken").value);

  getUserInfo($w("#sendToken").value)
    .then(usInfo => {
      ($w("#result").text) 
              });
}
//BACKEND CODE
import {fetch} from 'wix-fetch';


export function getUserInfo(token) {
    let atoken = token;
    let url = "https://api.pushbullet.com/v2/users/me";
      console.log("Retrieving information for  " + atoken);


   return fetch("https://api.pushbullet.comm/v2/users/me", {
  headers: {
    "Access-Token": atoken,
    "Content-Type": "application/json"
  }

  .then(response => response.json())
})}

Thanks in advance for your help! Greatly appreciated! -Malc

  • I think it may be something with the way my Header is sending? That's an un-educated guess however. But thats what my gut says – newbie_wannabe Jun 19 '19 at 22:45

1 Answers1

1

your Access-Token value is double quoted, try without.

Gabriel Lopes
  • 183
  • 3
  • 12
  • Unfortunately it did the same thing. Thank you for the suggestion. I am open to any and all other ideas too!! – newbie_wannabe Jun 20 '19 at 01:11
  • Updated code to reflect Gabriel Lopes advice. Still does the same thing unfortunately. – newbie_wannabe Jun 20 '19 at 18:59
  • you have unclosed braces return fetch("https://api.pushbullet.comm/v2/users/me", { headers: { "Access-Token": atoken, "Content-Type": "application/json" } } } – Gabriel Lopes Jun 20 '19 at 19:06
  • When you log atoken before return, it's undefined or what? – Gabriel Lopes Jun 20 '19 at 19:07
  • Weird, the brackets are closed in the code but didn't paste to here for some reason. I also tried removing the [0] as I don't believe the fetch brings back an array. It should return User Info in json format. I'll report the updated code. Also, atoken comes from the user input in a text element id #sendToken – newbie_wannabe Jun 20 '19 at 20:37
  • I guess the main problem to solve is how do I get what is returned from the fetch, which is the User Info, displayed in textbox element id #result. I'm unsure how to properly code the console stuff to take and display to an element – newbie_wannabe Jun 20 '19 at 20:53
  • it seems that you're returns a function, try to fetch and return response, inside of then. Like this. Fetch(URL, { Header { Access-token: atoken }} .then(response => return response.json()) – Gabriel Lopes Jun 20 '19 at 21:19
  • I plugged this code in (with the correct header) and I get Parsing error: Unexpected token { – newbie_wannabe Jun 20 '19 at 21:36
  • I think i made a mistake, the correct code is https://jsfiddle.net/biutas/qL6y149g/1 – Gabriel Lopes Jun 20 '19 at 21:48
  • Ok great! I think we're making progress... no error codes. But it still doesn't display anything except the placeholder text in my #result textbox. Could you take another glance at my front end code to see if it meshes well with the new backend code? – newbie_wannabe Jun 20 '19 at 22:04
  • Yeah, sure, inside the getUserInfo then, usInfo receive the response as JSON, so you you have to set input value as usInfo.name, for example. In Jquery you do like this $("#result").val(usInfo.name); I don't know the $w function, but probably is the same. – Gabriel Lopes Jun 20 '19 at 22:34
  • not sure if I put it in correctly... ``` import {getUserInfo} from 'backend/getuser'; export function button1_click(event, $w) { console.log("Retrieving information for " + $w("#sendToken").value); getUserInfo($w("#sendToken").value) .then(usInfo => { ($w("#result").value(usInfo.name)) }); } ``` I get an error warning that says ""value' does not exist on '#result'" I get a Wix Help Message over UserInfo that says "usInfo: wix_users.User.UserRole An object returned by the getRoles() function representing a user's roles." – newbie_wannabe Jun 21 '19 at 00:32
  • Gabriel Lopes, I put in: ``` import {getUserInfo} from 'backend/getuser'; export function button1_click(event, $w) { console.log("Retrieving information for " + $w("#sendToken").value); getUserInfo($w("#sendToken").value) .then(usInfo => {import {getUserInfo} from 'backend/getuser'; export function button1_click(event, $w) { console.log("Retrieving information for " + $w("#sendToken").value); getUserInfo($w("#sendToken").value) .then(usInfo => { $w("#result").value = usInfo.name; }); } ``` I got error: 'value' does not exist on '#result' – newbie_wannabe Jun 21 '19 at 02:55
  • IT WORKS!!!!!!!! YAAAAAAYYYYY!!!! Thank you soooo much Gabriel Lopes!!!! I so appreciate you!!!!!!! This is my first ever code =D I feel like a million bucks - thank you again!! – newbie_wannabe Jun 21 '19 at 04:34
  • I had to do one minor edit to the code you gave, it should've been .text vs .value ``` $w("#result").text = usInfo.name; ``` – newbie_wannabe Jun 21 '19 at 04:42
  • Awesome! it's the best feelling, isn't? I'm curious why you're using wix library, it's a wix website? – Gabriel Lopes Jun 21 '19 at 11:40
  • It indeed is! Now to learn more! Wix is a website builder like WordPress and the like. – newbie_wannabe Jun 21 '19 at 18:17