我把这一页作为一个起点。
我的代码如下所示:
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.CallerrorFromStatus(/workspace/node_modules/@grpc/grpc-js/build/src/client.js:31:26)
在Object.onReceivestatus(/workspace/node_modules/@grpc/grpc-js/build/src/client.js:176:52)
在
有什么想法吗?
将此作为社区维基发布,因为它基于@JimMorrison的评论。
这里的问题是,您在res.send()
中传递了一个数组,但它需要一个字符串(给定text/plain
内容类型),因此在这里使用它之前,您需要将这个数组转换为字符串。您可以通过使用以下代码来完成此操作:
res
.status(200)
.set('Content-Type', 'text/plain')
.send(JSON.stringify(resText))
.end();