ga-wdi-exercises / pbj-project3

[project]
0 stars 2 forks source link

accessing items in an object #31

Closed msteffan closed 9 years ago

msteffan commented 9 years ago

Following up on the spotify/users issue from earlier... We have a user object as follows: screen shot 2015-08-26 at 10 58 42 am

but it returns this error when we try to embed it in hbs: screen shot 2015-08-26 at 11 00 22 am

Everything we can find indicates that this is the right way to access the first item in the object, but when we call it as use[0]["spotifyId"] that also returns an error. We even tested it in repl.it and that works, but this call doesn't. Any ideas?

mattscilipoti commented 9 years ago

That code looks correct. The only conclusion I can draw is that the value of user is not what you expect at that point. tip: Please post code as code, using triple back-tics (```). It makes it much easier to test.

jshawl commented 9 years ago

unfortuanely, handlebars is extremely limited.

can you move the [0]["spotifyId"] to the res.locals function?

RobertAKARobin commented 9 years ago

Or do something like res.render("/someRoute", {user: user[0]["spotifyId"]}), so you can access {{user}}.

msteffan commented 9 years ago

We tested that previously, @jshawl. We used:

app.use(function(req, res, callback){
    console.log("req user is", req.user[0]["spotifyId"]);
    if (req.user){
        res.locals.user = req.user
        //console.log(req.user);
    }
    callback();
})

(and that's just to console.log the value of user[0]["spotifyId"]) it returns the same error: screen shot 2015-08-26 at 11 35 45 am

RobertAKARobin commented 9 years ago

I think that's from your console.log line. Try moving that inside your if statement. It's going to error if you try to call req.user[0] anywhere req.user isn't defined. If you move that inside the if, it ensures that you'll only call req.user[0] if req.user exists.

msteffan commented 9 years ago

Yes!! I created a new variable for spotifyId and now that works in hbs. THANK YOU, all!