简体   繁体   English

python龙卷风中的“ TypeError”简单get方法可访问Mongodb中的记录

[英]“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). 嗨,我最近开始用Python编程(我是python编程的新手)。 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. 我的MongoDB中只有一小部分数据,我编写了一个简单的get方法来查找我的数据集中的所有数据。 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 }) 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. RequestHandler.get()不应该返回任何内容。 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. 龙卷风处理程序通过调用self.write()而不是返回值来产生输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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