简体   繁体   中英

“TypeError” simple get method in python tornado to access record from Mongodb

Hi I have recently started programming in Python (I am newbie to python programming). I have a small collection of data in my MongoDB.I have written a simple get method to find all the data from my collection. But I have an error returning the fetched value.

Here is my code:

import bson
from bson import json_util
from bson.json_util import dumps

class TypeList(APIHandler):
    @gen.coroutine
    def get(self):
        doc = yield db.vtype.find_one()
        print(doc)
        a = self.write(json_util.dumps(doc))
        return a

    def options(self):
        pass

It gives me the fetched data.

But when I replace these lines

a = self.write.... return a

with return bson.json_util.dumps({ 'success': True, 'mycollectionKey': doc })

it gives me a type error.

TypeError: Expected None, got {'success': True, 'mycollectionKey': {'type': 1, 'item': 'cookie'}}

Can anyone explain me why I get this error and is there anyway to solve the problem.

Thanks in advance.

RequestHandler.get() is not supposed to return anything. This error is simply warning you that you returned a value that is being ignored. Tornado handlers produce output by calling self.write(), not by returning a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM