Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.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 使用数据存储';s分页示例导致“;错误:13内部:请求消息序列化失败:编码无效“;_Javascript_Node.js_Express_Google Cloud Datastore - Fatal编程技术网

Javascript 使用数据存储';s分页示例导致“;错误:13内部:请求消息序列化失败:编码无效“;

Javascript 使用数据存储';s分页示例导致“;错误:13内部:请求消息序列化失败:编码无效“;,javascript,node.js,express,google-cloud-datastore,Javascript,Node.js,Express,Google Cloud Datastore,我用它作为起点 我的代码如下所示: app.get('/gizmos', async (req, res, next) => { const pageSize = 3; const resText = []; const cursor = req.query.cursor; try { let query = datastore.createQuery('gizmo').limit(pageSize); if (cursor) { quer

我用它作为起点

我的代码如下所示:

app.get('/gizmos', async (req, res, next) => {

  const pageSize = 3;
  const resText = [];
  const cursor = req.query.cursor;

  try {
    let query = datastore.createQuery('gizmo').limit(pageSize);

    if (cursor) {
      query = query.start(cursor);
    }

    const [results] = await datastore.runQuery(query);
    const [gizmos] = results[0];

    const info = results[1];

    if (info.moreResults !== Datastore.NO_MORE_RESULTS) {
    // If there are more results to retrieve, the end cursor is
    // automatically set on `info`. To get this value directly, access
    // the `endCursor` property.
        const nextUrl = req.protocol + "://" + req.get("host") + req.baseUrl + "?cursor=" + info.endCursor;
        console.log(nextUrl);
    }

    gizmos.forEach(gizmo => {

      const id = gizmo[Datastore.KEY].id;

      resText.push({
         "id" : id,
         "name" : gizmo.name,
  
      });

    });

    res
      .status(200)
      .set('Content-Type', 'text/plain')
      .send(resText)
      .end();
  } catch (error) {
    next(error);
  }
});
但是它失败了,出现了
500
错误。日志说:

错误:13内部:请求消息序列化失败:无效 在Object.CallerRomStatus处编码 (/workspace/node_modules/@grpc/grpc js/build/src/call.js:31:26)
在Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc js/build/src/client.js:176:52)
在Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:336:141) 在Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:299:181) 在 /workspace/node_modules/@grpc/grpc js/build/src/call stream.js:145:78 在处理和拒绝时(内部/process/task_queues.js:75:11)


有什么想法吗?

将此作为社区维基发布,因为它基于@JimMorrison的评论

这里的问题是,您正在
res.send()
中传递一个数组,但鉴于
text/plain
内容类型,它需要一个字符串,因此您需要将此数组转换为字符串,然后再在此处使用它。您可以使用以下代码执行此操作:

res
  .status(200)
  .set('Content-Type', 'text/plain')
  .send(JSON.stringify(resText))
  .end();

随机猜测问题在于,您正在res.send()中传递一个数组,但res.send()需要一个字符串。send(JSON.stringify(resText))可能会修复它。是否正在记录这一行
console.log(nextur)?如果是这样的话,那么查询是正确执行的,原因可能是上面的注释。