Open UNIcodeX opened 4 years ago
Are you trying to choose the database to authenticate the connection against instead of the default admin
database?
It is my understanding that the authentication occurs during connect()
. You might try passing this information by using the URI in newMongoWithURI
rather than newMongo
.
Per the spec, you can choose both the database and auth-database like this:
mongodb://user:pass@192.168.0.127:27017/test
Or like this if the auth-database is not the same as the chosen database:
mongodb://user:pass@192.168.0.127:27017/test?authSource=otherdb
However, I've not checked if this second form works with this driver.
What I'm trying to do is use Mongo with Jester for a REST API. I've got two issues.
1) With known working credentials in the newAsyncMongoWithURI
method, nimongo says I must be authenticated to do operations on the database.
2) Jester does not know how to handle Bson in the response. Perhaps a toJson
method in the nimongo
or bson
libraries would be good for this.
There may be (probably is) a better way of doing this, so if you're aware of it, please advise.
At-ing @dom96 in case he has some insight into this.
Getting the following error when removing the Jester portion:
(ok: false, n: 0, err: "command insert requires authentication", inserted_ids: @[{"$oid": "5ee0fdfa56670000000020e9"}], bson: {
"ok" : 0.0,
"errmsg" : "command insert requires authentication",
"code" : 13,
"codeName" : "Unauthorized"
})
It looks like from the examples that using the URI method would auth on connection. Is this not correct?
Building the following with nim c -r -d:release -d:danger --threads:on testSrv.nim
import strformat
import jester
import nimongo/mongo
import nimongo/bson
var
user = "testUser"
pass = "testPass"
db = "test"
m: AsyncMongo = newAsyncMongoWithURI(fmt"mongodb://{user}:{pass}@192.168.0.127/{db}?authSource=admin")
coll = m["test"]["test"]
if not waitFor m.connect():
echo "Could not connect."
discard waitFor coll.insert(%*{"testKey": "test"})
routes:
get "/":
let res = waitFor coll.find(%*{}).all()
resp res
runForever()
Jester's error message
Error: type mismatch: got <seq[Bson]>
but expected one of:
template resp(code: HttpCode): typed
first type mismatch at position: 1
required type for code: HttpCode
but expression 'res' is of type: seq[Bson]
template resp(code: HttpCode; content: string;
contentType = "text/html;charset=utf-8"): typed
first type mismatch at position: 1
required type for code: HttpCode
but expression 'res' is of type: seq[Bson]
template resp(code: HttpCode; headers: openArray[tuple[key, value: string]];
content: string): typed
first type mismatch at position: 1
required type for code: HttpCode
but expression 'res' is of type: seq[Bson]
template resp(content: JsonNode): typed
first type mismatch at position: 1
required type for content: JsonNode
but expression 'res' is of type: seq[Bson]
template resp(content: string; contentType = "text/html;charset=utf-8"): typed
first type mismatch at position: 1
required type for content: string
but expression 'res' is of type: seq[Bson]
The Jester part is fairly straightforward. The resp
template only allows for a few parameter types. It does not support JSON or BSON, at least not directly. It is generally used with a string. So, converting the parameter passed into res
into a string will put the data into the web response.
To pass JSON or XML in a RESTful fashion, here is an snippet of code I used for generating a RSS feed:
routes:
get "/category/techpodcast/feed/":
var db = getNextConnection()
data["itemList"] = read_podCastItemList_recentFifteen(db)
resp Http200, rssPodcastXML(data), "application/rss+xml"
My rssPodcastXML
function returns a string of serialized XML.
(This particular code used mongopool
rather than nimongo
, but the idea is very similar.)
Describe the bug A clear and concise description of what the bug is. What am I doing wrong??
gives error:
To Reproduce Steps to reproduce the behavior:
Expected behavior A clear and concise description of what you expected to happen.
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
Smartphone (please complete the following information):
Additional context Add any other context about the problem here.