Bson error for adding comments

The further, I am getting the following order running the tests/def_raise_invalid_id
“bson.errors.InvalidId: ‘’ is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string”
Could this be the reason, why the comment cannot be created? Regards Tilo

Hi!,

Here, the problem is related to how to create/use ObjectId type in Python throughtout PyMongo library.

You can review this link for getting help: http://api.mongodb.com/python/current/api/bson/objectid.html

Regards.

Hi! I stumbled with this problem as well no matter if I put ObjectId(comment_id) or not in my method:

def update_comment(comment_id, user_email, text, date):
“”"
Updates the comment in the comment collection. Queries for the comment
based by both comment _id field as well as the email field to doubly ensure
the user has permission to edit this comment.
“”"
# TODO: Create/Update Comments
# Use the user_email and comment_id to select the proper comment, then
# update the “text” and “date” of the selected comment.
response = db.comments.update_one({“email”: user_email, “_id”: ObjectId(comment_id)}, {"$set": {“text”: text, “date”: date}})

return response

The only difference is that when I put “comment_id” without the ObjectId() method, a test shows passed

My teste didn’t pass. I tried with ObjectId() and without this method.

This is the message received at statu’s page.

Create/Update Comments: Cannot read property ‘_id’ of undefined

My update method:

def update_comment(comment_id, user_email, text, date):
“”"
Updates the comment in the comment collection. Queries for the comment
based by both comment _id field as well as the email field to doubly ensure
the user has permission to edit this comment.
“”"
# TODO: Create/Update Comments
# Use the user_email and comment_id to select the proper comment, then
# update the “text” and “date” of the selected comment.
response = db.comments.update_one({“email”: user_email, “_id” : ObjectId(comment_id)}, {"$set": {“text”: text, “date”: date}})
return response

I have the same problem running test_update_comment as well. The error message shows

@pytest.mark.create_update_comments
def test_update_comment(client):
    print('Comment[id]')
    print(comment['id'])
    result = update_comment(comment['id'], user.email, 'foo foo foo', now)
    assert result.acknowledged is True

    comments = get_movie(movie_id).get('comments')
    assert result.raw_result.get('nModified') == 0
>       assert comments[0].get('text') == 'foo foo foo'
E       AssertionError: assert 'fe-fi-fo-fum' == 'foo foo foo'
E         - fe-fi-fo-fum
E         + foo foo foo

It seems that the my update_comment method in db.py is not updating the right comment. Here is the code:

def update_comment(comment_id, user_email, text, date):
    response = db.comments.update_one({"_id": ObjectId(comment_id), "email": user_email}, {"$set": {"text": text, "date": date}})
    return response

I know that ObjectId(comment_id) should be correct because I printed out comment_id and it is a 12-byte ObjectId…

I cannot move on to the next test delete_comments without getting this test to pass first. Please help.

1 Like

@jzqc check your method add_coment if it is using the ObjectId one movie_id too. I was looking for an error in update, but my error was one the add method.

3 Likes

That was my problem as well, I fixed adding the ObjectId() on the add_comment method, for the movie_id field

2 Likes

This … solve my troubles. :wink:

Tks!!! solve my troubles too.

Hi there,

I have the same solution but my tests are failing with the BSON error. The error i am getting is,

oid = ‘’

def _raise_invalid_id(oid):
    raise InvalidId(
        "%r is not a valid ObjectId, it must be a 12-byte input"
      " or a 24-character hex string" % oid)

E bson.errors.InvalidId: ‘’ is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string

response = db.comments.update_one({“email”: user_email, “_id”: ObjectId(comment_id)}, {
“$set”: {“text”: text, “date”: date}})

I have also edited the add_comment method to use the ObjectId but i am still getting the error. Any suggestions , Thanks.

Correct, all _id should in ObjectId: "_id": ObjectId(comment_id)
If it’s doesn’t work, try to re-restore your database.

I am getting exactly the same error here. I have ensured all my ids are ObjectId format and yet my unit tests keep giving me the same error:

oid = ''

    def _raise_invalid_id(oid):
        raise InvalidId(
            "%r is not a valid ObjectId, it must be a 12-byte input"
>           " or a 24-character hex string" % oid)

Is too late in the day to investigate this issue My add_comment() looks like this:

comment_doc = { "name": user.name,
                "email": user.email,
                "movie_id": ObjectId(movie_id),
                "text": comment,
                "date": date
                }
return db.comments.insert_one(comment_doc)

and my update_comment():

response = db.comments.update_one({"id": ObjectId(comment_id), "email": user_email}, {
                                      "$set": {"text": text, "date": date}})

    return response

I’ve restored the data but nothing seems to fix this issue

same here :frowning: