meteor restivus 如何读取多个 queryParams
meteor restivus how to read multiple queryParams
我正在使用 Meteor 中的 Restivus 构建 API。
在自定义路由中,我希望像这样将多个值作为 queryParams(例如 value1 和 value2):
...domain/api/update?key=1234&value1=10
如何在端点函数中获取它们?
当我尝试这个时,我得到了 undefined:
var query = this.queryParams.key // result: 1234
var value1 = this.queryParams.value1 // result: undefined
更新
这是我的新代码,结果相同。
使用标准的 Meteor 项目。流星版本 1.0.3.2
// Create collection
Posts = new Mongo.Collection("posts");
if (Meteor.isServer) {
Meteor.startup(function () {
// RESTIVUS
// Global configuration
Restivus.configure({
useAuth: false,
prettyJson: true
});
// Given the url: "/posts?key=1234&value1=10"
Restivus.addRoute('posts', {
get: function () {
var key = this.queryParams.key;
var value1 = this.queryParams.value1;
console.log("key: " + key); // result: 1234
console.log("value1: " + value1); // result: undefined
}
});
});
}
这就是问题的解决方法。取自这里:
https://github.com/kahmali/meteor-restivus/issues/16
您正在使用 curl 进行测试,对吗?很明显(不要因为不知道这一点而难过,因为我也不知道),& 符号意味着前面的命令将在后台 运行 ,所以查询参数只是 t运行一旦 curl 命令到达第二个查询参数的 &。您所要做的就是将 URL 括在引号中,瞧!请尝试使用此命令:
curl "http://testivus.meteor.com/api/posts?key=1234&value1=10"
应该可以。因此,如果您刚刚将 URL 输入浏览器或使用更高级的 REST 客户端,您会看到定义了额外的查询参数。我从这个 Whosebug 问题中得到了答案。
我正在使用 Meteor 中的 Restivus 构建 API。
在自定义路由中,我希望像这样将多个值作为 queryParams(例如 value1 和 value2):
...domain/api/update?key=1234&value1=10
如何在端点函数中获取它们?
当我尝试这个时,我得到了 undefined:
var query = this.queryParams.key // result: 1234
var value1 = this.queryParams.value1 // result: undefined
更新 这是我的新代码,结果相同。 使用标准的 Meteor 项目。流星版本 1.0.3.2
// Create collection
Posts = new Mongo.Collection("posts");
if (Meteor.isServer) {
Meteor.startup(function () {
// RESTIVUS
// Global configuration
Restivus.configure({
useAuth: false,
prettyJson: true
});
// Given the url: "/posts?key=1234&value1=10"
Restivus.addRoute('posts', {
get: function () {
var key = this.queryParams.key;
var value1 = this.queryParams.value1;
console.log("key: " + key); // result: 1234
console.log("value1: " + value1); // result: undefined
}
});
});
}
这就是问题的解决方法。取自这里: https://github.com/kahmali/meteor-restivus/issues/16
您正在使用 curl 进行测试,对吗?很明显(不要因为不知道这一点而难过,因为我也不知道),& 符号意味着前面的命令将在后台 运行 ,所以查询参数只是 t运行一旦 curl 命令到达第二个查询参数的 &。您所要做的就是将 URL 括在引号中,瞧!请尝试使用此命令:
curl "http://testivus.meteor.com/api/posts?key=1234&value1=10"
应该可以。因此,如果您刚刚将 URL 输入浏览器或使用更高级的 REST 客户端,您会看到定义了额外的查询参数。我从这个 Whosebug 问题中得到了答案。