当前使用SuperAgent构造查询字符串的机制是什么

What is the current mechanism to construct a query string with SuperAgent?

本文关键字:字符串 查询 机制 是什么 SuperAgent      更新时间:2023-09-26

我正在尝试使用SuperAgent.data()为每个文档构造一个查询字符串。但是.data()似乎已不存在。

superagent
    .get(URL)
    .data({ 'screen_name': USER, 'count': '1' })
    .end(function(response){        
        if (response.ok) {
            console.log('yay got ' + JSON.stringify(response.body));
        } else {
            console.log('Oh no! error ' + response.text);
        }
});

结果:

Object #<Request> has no method 'data'

尝试用.send 替换.data

基于来源中的评论:

  /**
   * Send `data`, defaulting the `.type()` to "json" when
   * an object is given.
   *
   * Examples:
   *
   *       // querystring
   *       request.get('/search')
   *         .send({ search: 'query' })
   *         .end(callback)
   *
   *       // multiple data "writes"
   *       request.get('/search')
   *         .send({ search: 'query' })
   *         .send({ range: '1..5' })
   *         .send({ order: 'desc' })
   *         .end(callback)
   *
   *       // manual json
   *       request.post('/user')
   *         .type('json')
   *         .send('{"name":"tj"})
   *         .end(callback)
   *       
   *       // auto json
   *       request.post('/user')
   *         .send({ name: 'tj' })
   *         .end(callback)
   *       
   *       // manual x-www-form-urlencoded
   *       request.post('/user')
   *         .type('form')
   *         .send('name=tj')
   *         .end(callback)
   *       
   *       // auto x-www-form-urlencoded
   *       request.post('/user')
   *         .type('form')
   *         .send({ name: 'tj' })
   *         .end(callback)
   *
   * @param {String|Object} data
   * @return {Request} for chaining
   * @api public
   */

上面的答案在最新版本的SuperAgent(0.18.0)中对我不起作用。我不得不使用query函数(http://visionmedia.github.io/superagent/#query-字符串)。

request.get("/search").query({ search: 'query' }).end(callback);