Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法对回调函数进行分页_Javascript_Node.js_Express_Ecmascript 6 - Fatal编程技术网

Javascript 无法对回调函数进行分页

Javascript 无法对回调函数进行分页,javascript,node.js,express,ecmascript-6,Javascript,Node.js,Express,Ecmascript 6,我试图对回调函数的调用进行分页,但在第二次调用时出错 我的职能是: let content = '' let size = 100 let from = 1 function result(size, from, callback) { api.performRequest('/myContents', 'GET', { pageSize: size, startFrom: from,

我试图对回调函数的调用进行分页,但在第二次调用时出错

我的职能是:

        let content = ''

    let size = 100
    let from = 1

    function result(size, from, callback) {
        api.performRequest('/myContents', 'GET', {
            pageSize: size,
            startFrom: from,
            filter:'NOT contents:[* TO *]',
        }, function (data) {
            content += JSON.stringify(data)
            callback()
        })
    }

    function logContent() {
        const parsed = JSON.parse(content)
        console.log('content length: ', parsed.body.length)

        if (parsed.body.length === size) {
            calculate(size, from + size)
        }
    }

    function calculate(size, from) {
        result(size, from, logContent)
    }

    calculate(size, from)
在第一次调用时,控制台返回

内容长度:100

在第二次调用中,我在日志中获得了所有json块,并且出现了一个错误

JSON中位置32847处的意外标记{

我认为这与回调和函数完成之前发生的事情有关,但我看不出我在这里做错了什么

函数performRequest只执行一个http get并返回一个json块

export function performRequest(endpoint, method, data, callback) {

const headers = {
    Authorization: auth,
    'Content-Type': 'application/json; charset=utf-8',
    'Access-Control-Expose-Headers': 'Location,ETag',
}

let options = {
    host: host,
    port: port,
    path: endpoint,
    headers: headers,
}

if (method === 'GET') {
    endpoint += '?' + queryString.stringify(data)
    const additionalOptions = {
        path: endpoint,
    }

    options = _.merge(options, additionalOptions)
}

return http.get(options, function (response) {
    // Continuously update stream with data
    let body = ''
    response.on('data', function (d) {
        body += d
    })
    response.on('end', function () {

        // Data reception is done
        const parsed = JSON.parse(body)
        callback({
            totalCount: parsed.totalCount,
            body: parsed.content,
        })
    })
})
}

您的第一次通话不会有问题,因为您的内容一开始是空的。因此,在第一次通话中:

content = '' --> content = '{...}'
还有你的第二个电话:

content = '{...}' --> content = '{...}{...}'
因此,错误:

JSON中位置32847处的意外标记{


你需要将每个对象放入一个数组中,如果你想让它工作的话,甚至可以放入另一个对象中。你可以创建一个数组,并在每次调用时将每个元素推入其中。

你完全正确,tkx很多@Ksyqo。重置内容或使用数组都可以很好地工作。