我不是RethinkDB专家,但我可能会对此问题做一个简单的浅析浅析/修复(假设您正在使用JavaScript)。
Why doesn't it work with req('url')
and req('options')
in the loop then?
我不认为这是一个循环有关的问题,它更可能与ReQL表情和可能可选参数的方式RethinkDB交易的(如果以错误信息考虑在内)。 req('url')
和req('options')
都是ReQL表达式,可以使用r.expr(...)
手动实例化。
考虑下面的代码,以你的问题面临的还有:
// These two are your req('url') and req('options') equivalents respectively
const url = r.expr('https://postman-echo.com/get');
const options = r.expr({ params: { foo: 1, bar: 2 } });
// ...
r.http(url, options);
e: Expected 1 argument (not including options) but found 2.
好吧,让我们尽量不使用简单r.http(url)
的options
参数运行它,这就是它得到:
{
"args": { },
"headers": {
"accept": "*/*",
"accept-encoding": "deflate, gzip",
"host": "postman-echo.com",
"user-agent": "RethinkDB/2.3.2-windows-beta-472-ga2117b",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
} ,
"url": "https://postman-echo.com/get"
}
我相信r.expr(...)
-instantiated ReQL值与options
在检测到is-this-option时可能发生冲突(可能是由于t o与undefined
相同的行为)。 您只需将文档属性封装在原始JavaScript对象中,因为RethinkDB似乎可以与嵌套属性完美协作。
r.http(url, rebuildOptions(options));
其中rebuildOptions
可以被实施为相当有限的功能由于r.http()
API限制,例如:
const rebuildOptions = options => ({
method: options('method').default('GET'), // default GET is fine for method
// auth: don't know how to deal with auth - we cannot assign undefined to the property and cannot provide the default auth too
params: options('params').default({}), // default {} is fine for params
header: options('header').default({}), // the headers will be merged in the final request
// data: don't know how to deal with data too - we can analyze options('method'), but cannot assign a good default value
});
脏和弱。 我也可以提出一个更“hackish的”和更通用的实现,我在考察ReQL发现值结构:
// This looks to work with any ReQL values
const unexpr = expr => Object.assign({}, expr.optargs);
r.http(url, unexpr(options));
现在都可以实现返回:
{
"args": {
"bar": "2",
"foo": "1"
} ,
"headers": {
"accept": "*/*",
"accept-encoding": "deflate, gzip",
"host": "postman-echo.com",
"user-agent": "RethinkDB/2.3.2-windows-beta-472-ga2117b",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
} ,
"url": "https://postman-echo.com/get?bar=2&foo=1"
}
它看起来就像一个设计不完美(r.expr()
没有它的“unwrapper”副本,如果RethinkDB有需要的话)或可选参数检测错误,并且可能值得提交到RethinkDB issue tracker至少得到Re thinkDB团队反馈。
我希望我可以表达这个答案对我来说有多快乐 - 看起来我们在尝试解决这个问题时采用了相同的路径 - 我使用r.http(req('url'),{auth:req ('options')('auth'),params:req('options')('params')})' - 这是您的'rebuildOptions'解决方案的更糟糕的版本 - 我有相同的预感wrt“可选参数检测错误“和”r.expr“的设计不完美......因此,您的'unexpr'解决方案*非常吸引人;也很独特;一个非常优雅的解决方法 - 感谢堆^ _^ – naomik
我[在这里提交了一个问题](https://github.com/rethinkdb/rethinkdb/issues/6523) - 也许你想贡献你的帖子到github线程呢?我将把这个问题标记为答案。 – naomik
SamHughes告诉我,'unexpr'只能在这里工作,因为这个对象是用'R.expr'构造的;在真正的查询中,这不会**工作 - 'rebuildOptions'方法是这样做的正确方法,但是Sam还会使用'.do'来优化其性能。看到他的回答详情 – naomik