0

I am trying to determine if a document (received as part of an array of objects) exists in a collection. If it does not exist, I wish to insert the document and add "unread = true" to the object.

Code in coffee script for the above is as follows:

updateEvents = (db, events, done) ->

async.each events, (eventobj, cb) ->
    # check whether event exists in event collection
    db.Event.count eventobj, (err, count) ->
        # if it doesn't
        if count is 0
            # insert the document and when finished set an unread property on this object to true
            db.Event.insert eventobj, (err) ->
                eventobj.unread = true
        # if it does, log that is was found
        else
            console.log "yep, found it"

# pass results back up chain to client
done null, events

The unread property appears on the eventobj when console logged after assignment, however, it does not persist when passed to the client. It is as if the assignment never happened. Why is this occurring?

This is different than the type error question as it involves a data flow issue and not an error of any kind.

2
  • Possible duplicate of mongo/node TypeError: callback is not a function on query Commented Jan 23, 2016 at 22:17
  • Different question concerning a similar problem. This has nothing to do with errors, but with how callbacks work. Commented Jan 23, 2016 at 23:22

3 Answers 3

0

You should set the unread to true before insert operation.

eventobj.unread = true
db.Event.insert eventobj, (err) ->
    // error handling

rather than set the unread in the callback function. When you set it in the callback function, the eventobj has already been saved into db, and just change the eventobj itself.

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately, that doesn't do it either. I can get it working erratically, I think the async.series is not finishing before all the assignments have been evaluated.
@Nick, maybe you are right.. But async.series? I just find async.each in your codes.
0

As zangw said you should set the property before the insert if you are wanting to save the unread property in the db. What I take away from your question is that you just want to return the events array with an unread property appended to every object which doesn't exist in the db already. In either case you should update the actual events array to return. Instead of:

eventobj.unread = true

Do:

var index = events.indexOf(eventobj);

if (index !== -1) {
  events[index].unread = true;
}

5 Comments

Pardon the lack of coffee script.. I never use it :)
I am looking to insert the event as is into the database, AND append an unread property to each event in the events array which is NOT found in the database. Basically, receive events, check each event against db records, if not in db, put it in, tag it with unread and pass it up the chain.
OK so if you add the code as I suggested you should be able to pass the events array (with respective unread properties) up the chain.
Unfortunately, it is not working. To be more precise, ~5% of events are being flagged despite having an empty collection. When the collection is empty, %100 of events should be flagged. it would seem that the assignment is not happening on the majority of events, why I can only guess (something asynchronous, no doubt).
Do you have a unique id on the event objects? Doing it this way may have quirky results with duplicate entries in the events array.
0

Solved. The problem was the async.each was not finished executing when the done callback was being invoked. I put the done callback into an anonymous function and appended it as the optional callback to the async.each function.

updateEvents = (db, events, done) ->

async.each events, ((eventobj, cb) ->
    # check each event to see if it exists in users collection
    db.Event.count eventobj, (err, count) ->
        # if it doesn't
        if count is 0
            # # insert the document and when finished set an unread property on this object to true
            db.Event.insert eventobj
            eventobj.unread = true
            cb()
        # if it does, log that is was found
        else
            console.log "yep, found it"
            cb()
# pass results back up chain to client
), finished = -> done null, events

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.