用node.js从ravenDB中没有结果。

问题描述 投票:0回答:1

在使用其他语言6年之后,我试图用NoSQL DB回到node.js上。我已经实现了第一个简短的应用程序,我想通过一个给定的值从ravenDB数据库中获取一个对象。我想通过使用IATA代码来搜索它们,我的小程序看起来像这样。

var express = require("express");
var app = express();
app.listen(3000, () => {
    console.log("Server running on port 3000");
});

const { DocumentStore } = require('ravendb');
const store = new DocumentStore('http://localhost:5555', 'airportlist');
store.initialize();
const session = store.openSession();

app.get("/airportByIata/:iata", (req, res, next) => {
    var airport = session.query({ collection: "airports" }).whereEquals("iatacode", req.params.iata).first();
    res.json(JSON.parse(JSON.safeStringify(airport)));
});

这总是返回一个空的对象,但是当我在ravenDB中直接使用查询时,我得到了结果。 我的查询: "from 'airports' where iatacode = 'CGN'"

如果我只要求集合而不要求where子句(var airport = session.query({ collection: "airports" });)然后我得到以下结果。

{
"_events": {},
"_eventsCount": 1,
"_aliasToGroupByFieldName": {},
"_defaultOperator": "AND",
"_rootTypes": {},
"_queryParameters": {},
"_selectTokens": [],
"_whereTokens": [],
"_groupByTokens": [],
"_orderByTokens": [],
"_documentIncludes": {},
"_queryStats": {},
"_highlightingTokens": [],
"_queryHighlightings": {
"_highlightings": []
},
"_isGroupBy": false,
"_collectionName": "airports",
"_fromToken": {
"_collectionName": "airports",
"_dynamic": true,
"_alias": null
},
"_theSession": {
"_events": {},
"_eventsCount": 0,
"_clientSessionId": 1,
"_pendingLazyOperations": [],
"_hash": 1,
"_jsonSerializer": {
"_reviverRules": [
{}
],
"_replacerRules": [
{}
]
},
"deletedEntities": {},
"_knownMissingIds": {},
"documentsById": {
"_inner": {
"keysCaseSensitive": false
}
},
"includedDocumentsById": {
"keysCaseSensitive": false
},
"documentsByEntity": {},
"_numberOfRequests": 0,
"_deferredCommands": [],
"deferredCommandsMap": {},
"_generateDocumentKeysOnStore": true,
"_id": "4a84549a-0b84-4033-845a-79d8d4955e41",
"_databaseName": "airportlist",
"_documentStore": {
"_events": {},
"_eventsCount": 0,
"_urls": [
"http://localhost:5555"
],
"_lastRaftIndexPerDatabase": {
"keysCaseSensitive": false
},
"_eventHandlers": [],
"_subscriptions": {
"_subscriptions": {}
},
"_log": {},
"_databaseChanges": {},
"_requestExecutors": {},
"_database": "airportlist",
"_conventions": {
"_listOfQueryValueToObjectConverters": [],
"_registeredIdConventions": {},
"_registeredIdPropertyNames": {},
"_idPropertyCache": {},
"_readBalanceBehavior": "None",
"_identityPartsSeparator": "/",
"_identityProperty": "id",
"_maxNumberOfRequestsPerSession": 30,
"_maxHttpCacheSize": 134217728,
"_knownEntityTypes": {},
"_objectMapper": {
"_throwMappingErrors": false,
"_dateFormat": "YYYY-MM-DDTHH:mm:ss.SSS0000"
},
"_useCompression": null,
"_dateUtilOpts": {},
"_dateUtil": {},
"_frozen": true
},
"_multiDbHiLo": {
"_generators": {},
"_dbName": "airportlist"
},
"_initialized": true
},
"_requestExecutor": {
"_updateDatabaseTopologySemaphore": {
"capacity": 1,
"current": 0,
"queue": [],
"firstHere": false
},
"_updateClientConfigurationSemaphore": {
"capacity": 1,
"current": 0,
"queue": [],
"firstHere": false
},
"_failedNodesTimers": {},
"_certificate": null,
"aggressiveCaching": null,
"numberOfServerRequests": 1,
"_clientConfigurationEtag": 0,
"_topologyEtag": 16,
"_log": {},
"_cache": {
"_items": {}
},
"_readBalanceBehavior": "None",
"_databaseName": "airportlist",
"_lastReturnedResponse": "2020-05-02T15:05:01.288Z",
"_conventions": {
"_readBalanceBehavior": "None",
"_identityPartsSeparator": "/",
"_identityProperty": "id",
"_maxNumberOfRequestsPerSession": 30,
"_maxHttpCacheSize": 134217728,
"_useCompression": null,
"_frozen": true
},
"_defaultRequestOptions": {
"gzip": true
},
"_firstTopologyUpdatePromiseInternal": {},
"_firstTopologyUpdateStatus": {
"_status": "RESOLVED"
},
"_nodeSelector": {
"_state": {
"speedTestMode": 1,
"topology": {
"etag": 16,
"nodes": [
{
"clusterTag": "A",
"url": "http://localhost:5555",
"serverRole": "Member",
"database": "airportlist"
}
]
},
"failures": [
0
],
"fastestRecords": [
0
]
}
},
"_updateTopologyTimer": {
"_periodInMs": 60000,
"_firstTimeDelayId": {
"_idleTimeout": 60000,
"_idlePrev": {
"expiry": 60877,
"id": -9007199254740990,
"msecs": 60000,
"priorityQueuePosition": 3
},
"_idleStart": 877,
"_repeat": null,
"_destroyed": false
}
},
"_topologyTakenFromNode": {
"database": "airportlist",
"url": "http://localhost:5555"
}
},
"maxNumberOfRequestsPerSession": 30,
"_generateEntityIdOnTheClient": {},
"_entityToJson": {
"_missingDictionary": {}
},
"_sessionInfo": {
"_sessionId": 1,
"_lastClusterTransactionIndex": null,
"_noCaching": false
},
"_valsCount": 0,
"_customCount": 0
}
}

我到底做错了什么?

node.js ravendb
1个回答
2
投票

问题来自于调用的异步性,第二部分在第一部分完成之前就被触发了。正确的解决方案是用async重做get请求。

© www.soinside.com 2019 - 2024. All rights reserved.