0

I am trying to import values for coordinates using getJson however the map doesn't load now and if I try to print the coordinates in an alert box for testing purposes, it says 'undefined'.

// get home from application.py using getjson
    var coordinates;
    $.getJSON("/variables", function(info) {
        coordinates = {lat: info.latitude, {c: 21}, lng: info.longitude};

        alert(info.latitude);
        /// Options for map
        // https://developers.google.com/maps/documentation/javascript/reference#MapOptions
        let options = {
            center: coordinates, // London, England
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            maxZoom: 14,
            minZoom: 13,
            panControl: true,
            styles: styles,
            zoom: 13,
            zoomControl: true
        };

  // rest of code which instantiates map has been omitted from this question

});

These variables do however exist, as when I try print the same variables in the python 'variables' function which getJson calls, they print successfully so I'm not sure what's wrong with this code.

@app.route("/variables")
@login_required
def variables():

    c = request.args.get("c")
    h = request.args.get("h")
    x = request.args.get("x")
    y = request.args.get("y")
    p = request.args.get("place")

    # if home info has been changed
    if x and y and h:
        db.execute("UPDATE users SET home = :home, latitude = :lat, longitude = :longitude WHERE id = :id", home=h, lat=x, longitude=y, id=session["user_id"])

        # update session["home_name"] for place_name so can be used as placeholder for location search box
        session["home_name"] = h
        return redirect("/")
    elif p:
        rating = db.execute("SELECT rating FROM rater WHERE location = :location", location=p)

        session["restaurant"] = p

        number = 0
        review = 0
        for i in rating:
            review += rating[i]["rating"]
            number += 1

        review = review / number
        return review
    elif c:
        info = db.execute("SELECT * FROM users WHERE id = :id", id=session["user_id"])
        # send info to scripts.js with jsonify
        return jsonify(info)
    else:
        return redirect("/")
3
  • isn't the getJSON call missing a data parameter? – DinoCoderSaurus Aug 30 '18 at 17:28
  • i didnt think you had to have that parameter – M. Alex Aug 31 '18 at 8:23
  • i've added more code including a data parameter but it doesn't load at all – M. Alex Aug 31 '18 at 8:36
0

return jsonify(info) returns a list, so there is no info.latitude. There would be info[0].latitude if any data is returned from variables.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .