无法检索对象的键的值->未知类型

时间:2019-05-08 20:34:45

标签: javascript mongodb

db.patches.insert({ _id: 0, name:"n/a",os:"n/a",active:"n/a",created_at:"n/a", modified_at:"n/a"})

var a = db.patches.find().sort({_id:-1}).limit(1)

a

print(a["name"]) //unknown type

这是我得到的JS对象 { "_id" : 0, "name" : "n/a", "publish_date" : "n/a", "status" : "n/a", "type" : "n/a", "release_schedule_id" : "n/a", "created_at" : "n/a", "modified_at" : "n/a" }

但是当我运行print(a["name"])时,它会显示unknown type

1 个答案:

答案 0 :(得分:0)

您是想做server吗? (patches而非cursor.next()

无论如何,find returns a cursor。游标的工作方式是,当您调用它时,它会打印它指向的当前文档并移至下一个文档,因此您需要对其进行遍历,并调用var a = db.patches.find().sort({_id:-1}).limit(1) a

做事时

a.hasNext()

您正在打印光标,现在false等于var a = db.patches.find().sort({_id:-1}).limit(1) print(a.next().name)

您想要做的是:

var a = db.patches.find().sort({_id:-1}).limit(1) print(a.next()['name'])

n/a

以上任何一项都将打印{{1}}。