gpt4 book ai didi

node.js - 异步等待样式调用适用于 nano.db.insert 但不适用于 nano.db.list

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:43 37 4
gpt4 key购买 nike

我正在使用https://github.com/apache/couchdb-nano在我的 nodeJS 项目中与我的 couchdb 实例交互。

const nano = = require('nano')('http://127.0.0.1:5984');
const eventsDb = nano.db.use('events');
const savedEvent = await eventsDb.insert({title: 'ABC'});
console.log('savedEvent:');
console.log(savedEvent.body);

以上有效,但以下无效(即它记录未定义):

const databases = await nano.db.list();
console.log(databases.body);
console.log(databases.rows);

下面打印一条很长的痕迹:

const databases = await nano.db.list();
console.log(databases);

上面的最后一个打印以下内容:

Request {
_events: [Object: null prototype] { pipe: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
method: 'GET',
headers:
{ 'content-type': 'application/json',
accept: 'application/json',
host: '127.0.0.1:5984' },
uri:
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: '127.0.0.1:5984',
port: '5984',
hostname: '127.0.0.1',
hash: null,
search: null,
query: null,
pathname: '/_all_dbs',
path: '/_all_dbs',
href: 'http://127.0.0.1:5984/_all_dbs' },
readable: true,
writable: true,
explicitMethod: true,
_qs:
Querystring {
request: [Circular],
lib:
{ formats: [Object], parse: [Function], stringify: [Function] },
useQuerystring: undefined,
parseOptions: {},
stringifyOptions: {} },
_auth:
Auth {
request: [Circular],
hasAuth: false,
sentAuth: false,
bearerToken: null,
user: null,
pass: null },
_oauth: OAuth { request: [Circular], params: null },
_multipart:
Multipart {
request: [Circular],
boundary: '85e773c9-18a7-4dee-8564-776339438318',
chunked: false,
body: null },
_redirect:
Redirect {
request: [Circular],
followRedirect: true,
followRedirects: true,
followAllRedirects: false,
followOriginalHttpMethod: false,
allowRedirect: [Function],
maxRedirects: 10,
redirects: [],
redirectsFollowed: 0,
removeRefererHeader: false },
_tunnel:
Tunnel {
request: [Circular],
proxyHeaderWhiteList:
[ 'accept',
'accept-charset',
'accept-encoding',
'accept-language',
'accept-ranges',
'cache-control',
'content-encoding',
'content-language',
'content-location',
'content-md5',
'content-range',
'content-type',
'connection',
'date',
'expect',
'max-forwards',
'pragma',
'referer',
'te',
'user-agent',
'via' ],
proxyHeaderExclusiveList: [] },
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
proxy: null,
tunnel: false,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: false,
port: '5984',
host: '127.0.0.1',
path: '/_all_dbs',
httpModule:
{ _connectionListener: [Function: connectionListener],
METHODS:
[ 'ACL',
'BIND',
'CHECKOUT',
'CONNECT',
'COPY',
'DELETE',
'GET',
'HEAD',
'LINK',
'LOCK',
'M-SEARCH',
'MERGE',
'MKACTIVITY',
'MKCALENDAR',
'MKCOL',
'MOVE',
'NOTIFY',
'OPTIONS',
'PATCH',
'POST',
'PROPFIND',
'PROPPATCH',
'PURGE',
'PUT',
'REBIND',
'REPORT',
'SEARCH',
'SOURCE',
'SUBSCRIBE',
'TRACE',
'UNBIND',
'UNLINK',
'UNLOCK',
'UNSUBSCRIBE' ],
STATUS_CODES:
{ '100': 'Continue',
'101': 'Switching Protocols',
'102': 'Processing',
'103': 'Early Hints',
'200': 'OK',
'201': 'Created',
'202': 'Accepted',
'203': 'Non-Authoritative Information',
'204': 'No Content',
'205': 'Reset Content',
'206': 'Partial Content',
'207': 'Multi-Status',
'208': 'Already Reported',
'226': 'IM Used',
'300': 'Multiple Choices',
'301': 'Moved Permanently',
'302': 'Found',
'303': 'See Other',
'304': 'Not Modified',
'305': 'Use Proxy',
'307': 'Temporary Redirect',
'308': 'Permanent Redirect',
'400': 'Bad Request',
'401': 'Unauthorized',
'402': 'Payment Required',
'403': 'Forbidden',
'404': 'Not Found',
'405': 'Method Not Allowed',
'406': 'Not Acceptable',
'407': 'Proxy Authentication Required',
'408': 'Request Timeout',
'409': 'Conflict',
'410': 'Gone',
'411': 'Length Required',
'412': 'Precondition Failed',
'413': 'Payload Too Large',
'414': 'URI Too Long',
'415': 'Unsupported Media Type',
'416': 'Range Not Satisfiable',
'417': 'Expectation Failed',
'418': 'I\'m a Teapot',
'421': 'Misdirected Request',
'422': 'Unprocessable Entity',
'423': 'Locked',
'424': 'Failed Dependency',
'425': 'Unordered Collection',
'426': 'Upgrade Required',
'428': 'Precondition Required',
'429': 'Too Many Requests',
'431': 'Request Header Fields Too Large',
'451': 'Unavailable For Legal Reasons',
'500': 'Internal Server Error',
'501': 'Not Implemented',
'502': 'Bad Gateway',
'503': 'Service Unavailable',
'504': 'Gateway Timeout',
'505': 'HTTP Version Not Supported',
'506': 'Variant Also Negotiates',
'507': 'Insufficient Storage',
'508': 'Loop Detected',
'509': 'Bandwidth Limit Exceeded',
'510': 'Not Extended',
'511': 'Network Authentication Required' },
Agent:
{ [Function: Agent] super_: [Function], defaultMaxSockets: Infinity },
ClientRequest: { [Function: ClientRequest] super_: [Function] },
globalAgent:
Agent {
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 80,
protocol: 'http:',
options: [Object],
requests: {},
sockets: {},
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256 },
IncomingMessage: { [Function: IncomingMessage] super_: [Function] },
OutgoingMessage: { [Function: OutgoingMessage] super_: [Function] },
Server: { [Function: Server] super_: [Function] },
ServerResponse: { [Function: ServerResponse] super_: [Function] },
createServer: [Function: createServer],
get: [Function: get],
request: [Function: request],
maxHeaderSize: [Getter] },
agentClass:
{ [Function: Agent]
super_:
{ [Function: EventEmitter]
EventEmitter: [Circular],
usingDomains: false,
defaultMaxListeners: [Getter/Setter],
init: [Function],
listenerCount: [Function] },
defaultMaxSockets: Infinity },
agent:
Agent {
_events: [Object: null prototype] { free: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 80,
protocol: 'http:',
options: { path: null },
requests: {},
sockets: {},
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256 } }

有人可以帮忙吗?所有 nano api 都以异步/等待方式工作吗?

最佳答案

最后一个堆栈跟踪告诉我,您正在 databases 变量中接收到一个 Request 对象。这是 7.0.0 版本之前的 Nano 的默认行为。

如果您使用最新的 Nano(在撰写本文时为 v8.0.1),那么大多数 Nano 函数都会返回 Promise 而不是 Request 对象 - 这将使您的异步/等待代码正常工作,即您的变量 databases 当 Promise 解析时将收到 HTTP 请求的正文。

为了确保您拥有最新的 Nano,请使用 npm install nano 或修改项目的 package.json 文件,以确保在运行 之前请求的版本号是最新的npm 安装

关于node.js - 异步等待样式调用适用于 nano.db.insert 但不适用于 nano.db.list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55786744/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com