如何获取collection.find(connect.data).fetch()的值?

时间:2018-04-05 13:09:23

标签: rest api http meteor

我正在尝试根据此RESTful API在线教程为我的应用创建一个流星The Meteor Chef。 HTTP包安装在本教程的开头,以便在API开发完成后测试RESTful API。

我目前正处于测试阶段,似乎无法让我的GET Methods用于从我的集合中检索数据。

在下面找到我的GET方法代码:

methods: {
   pescrow: {
     GET: function( context, connection ) {

    var hasQuery = API.utility.hasData( connection.data );
    console.log("hasQuery value == " +hasQuery+ " on line 183");

      if ( hasQuery ) {
        connection.data.owner = connection.owner;
        console.log("Your in GET::hasQuery: Line 187 " + connection.data );

        var getPescrows = recipientsDetails.find( connection.data ).fetch();             
        console.log("getPescrows value: " +getPescrows+ " Line 203");


      if ( getPescrows.length > 0 ) {
        // We found some pescrows, we can pass a 200 (success) and return the
        // found pescrows.
        console.log("getPescrows found Line 205");
        API.utility.response( context, 200, getPescrows );
        } 
        else {
        console.log("getPescrows NOT found Line 208!");
        // Bummer, we didn't find any pescrows. We can pass a 404 (not found)
        // and return an error message.
        API.utility.response( context, 404, { error: 404, message: "No Pescrows found, dude." } );
        }
        } 
        else {
      // Our request didn't contain any params, so we'll just return all of
      // the pescrows we have for the owner associated with the passed API key.
        var getPescrows = recipientsDetails.find( { "owner": connection.owner } ).fetch();
        API.utility.response( context, 200, getPescrows );
      }
    }
  }
}

我通过粘贴以下代码在Chrome控制台上测试我的API:

HTTP.get( "http://localhost:8000/paymentC2B/v1", { 
params: { 
"api_key": "b21d83ef267bd829a9d732551270c718",
"paymentStatus": "Pending",                                                                      
"recipientNumber" : "0705087633"
 } 
 }, function( error, response ) {
 if ( error ) {
 console.log( error );
 } else {
 console.log( response );
 }
 });

我在终端收到的回复是:

hasQuery == true Line 183
Your in GET::hasQuery: Line 187 [object Object]
getPescrows value:  Line 203
getPescrows NOT found Line 208!

当我在控制台中运行下面的查询时,它成功地产生:

    recipientsDetails.find({paymentStatus:"Pending", recipientNumber: "0705087633"}, {sort: {paymentDate: 'desc' }}).fetch()

显示:

[{…}]
0
:
key : "b21d83ef267bd829a9d732551270c718"
paymentDate : "2018-04-02 15:15:49"
paymentStatus : "Pending"
recipientAmount : "500"
recipientNumber : "0705087633"
_id : "uSsCbdBmmhR2AF2cy"
__proto__ : Object
length : 1
__proto__ : Array(0)

似乎问题出在recipientsDetails.find( connection.data ).fetch();查询中。有人可以在我的代码中指出我出错的地方吗?

期待您的回复。

1 个答案:

答案 0 :(得分:1)

当您测试params包含api_key时。我认为此密钥不会出现在您的recipientsDetails集合中。

而不仅仅是:

connection.data.owner = connection.owner;

尝试:

connection.data.owner = connection.owner;
delete connection.data.api_key;