> I suggesting adding printlns or logging or a debugger 
> and checking the 
> value of this-users-params, it is almost certainly not 
> what you expect 
> it to be. 

I already showed the output of the println statements I'd done, but I'll go 
through this again. 

I eventually try to output my info as JSON, like this:

(defn current-users [request]
  "The default action of this app. Add new users to the registry, and 
delete the ones that are more than 15 seconds old"
  (let [this-users-params (:params request)]
    (add-to-logged-in-registry this-users-params)
    (remove-old-registrants)
    (response (apply str (json/write-str @registry)))))

When I ran this at the REPL, everything worked great, but I was feeding it 
ideal information (no nil values). When I uploaded it to the server and the 
site users started hitting this thing (the Javascript in their browsers 
sent all kinds of calls). Then I started getting strange values, including 
a lot of empty calls. I can fix the Javascript, and I will, but I decided I 
also needed to protect the Clojure code. 

I originally had this:

(def registry (atom {}))

(defn add-to-logged-in-registry [this-users-params]
  (let [right-now (. (Date.) getTime)
        new-user-entry (conj this-users-params { "updated" right-now })]
    (println apply str new-user-entry)
      (swap! registry assoc (:username new-user-entry) new-user-entry)))

But on the server I would get errors like: 

2012-10-19 
02:01:26.214:WARN:oejs.AbstractHttpConnection:/?username=karlorihoo&first_name=Karlo&last_name=Rihoo&user_image=java.lang.Exception:
 
JSON object properties may not be nil
    at clojure.data.json$default_write_key_fn.invoke(json.clj:28)
    at clojure.data.json$write_object.invoke(json.clj:313)
    at clojure.data.json$fn__109$G__104__116.invoke(json.clj:279)

So I wanted to add this if statement:

(defn add-to-logged-in-registry [this-users-params]
  "We assume some user is looking at a site such as wpquestions.com and the 
Javascript on that site is sending an Ajax request to this app, every 10 
seconds, with a map of information about the user, which we need to store 
in the registry."
  (let [right-now (. (Date.) getTime)
        new-user-entry (conj this-users-params { "updated" right-now })]
    (println apply str new-user-entry)
    (if-not (nil? (:username new-user-entry))
      (swap! registry assoc (:username new-user-entry) new-user-entry))))

But then the line with swap! never seemed to get called and the registry 
remained empty no matter what I threw at this, including hardcoded "nil"s. 









On Friday, October 19, 2012 3:53:26 AM UTC-4, red...@gmail.com wrote:
>
> conj can surely produce maps, and does so happily in the following cases: 
>
> (conj {} [:foo :bar]) 
> (conj {} {:foo :bar}) 
>
> I suggesting adding printlns or logging or a debugger and checking the 
> value of this-users-params, it is almost certainly not what you expect 
> it to be. 
>
> as a side note creating a date object just to call getTime is kind of 
> gross, try (System/currentTimeMillis) 
>
> On Fri, Oct 19, 2012 at 12:42 AM, Sean Corfield 
> <seanco...@gmail.com<javascript:>> 
> wrote: 
> > On Fri, Oct 19, 2012 at 12:10 AM, larry google groups 
> > <lawrenc...@gmail.com <javascript:>> wrote: 
> >> 
> >> (defn add-to-logged-in-registry [this-users-params] 
> >>   (let [right-now (. (Date.) getTime) 
> >>         new-user-entry (conj this-users-params { "updated" right-now 
> })] 
> >>     (if (:username new-user-entry) 
> >>       (swap! registry assoc (:username new-user-entry) 
> new-user-entry)))) 
> >> 
> >> The if statement seems to never be true. 
> > 
> > 
> > conj produces a sequence, not a map, so the lookup of :username fails. 
> Try 
> > new-user-entry (assoc this-users-params "updated" right-now) 
> > -- 
> > Sean A Corfield -- (904) 302-SEAN 
> > An Architect's View -- http://corfield.org/ 
> > World Singles, LLC. -- http://worldsingles.com/ 
> > 
> > "Perfection is the enemy of the good." 
> > -- Gustave Flaubert, French realist novelist (1821-1880) 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> > Groups "Clojure" group. 
> > To post to this group, send email to clo...@googlegroups.com<javascript:> 
> > Note that posts from new members are moderated - please be patient with 
> your 
> > first post. 
> > To unsubscribe from this group, send email to 
> > clojure+u...@googlegroups.com <javascript:> 
> > For more options, visit this group at 
> > http://groups.google.com/group/clojure?hl=en 
>
>
>
> -- 
> And what is good, Phaedrus, 
> And what is not good— 
> Need we ask anyone to tell us these things? 
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to