Undefined when trying to access data in resultset after find() operation against collection
Hi there,
I have got my lokijs db up and running using a json on my filesystem with nodejs. I can see that the json is created and that the an initial row i insert is in the json. When i try to read the JSON i keep getting undefined whilst trying to retrieve a value for item in my resultset. If you can have a quick look below and provide an pointers that would be great
JSON is:
{
"filename": "lastfileplayed.json",
"collections": [
{
"name": "lastfileplayed",
"data": [
{
"name": "00001.wav",
"lastplayed": "correct",
"meta": {
"revision": 7,
"created": 1511381542440,
"version": 0,
"updated": 1511382869964
},
"$loki": 1
}
],
"idIndex": [
1
],
"binaryIndices": {
},
"constraints": null,
"uniqueNames": [
],
"transforms": {
},
"objType": "lastfileplayed",
"dirty": true,
"cachedIndex": null,
"cachedBinaryIndex": null,
"cachedData": null,
"adaptiveBinaryIndices": true,
"transactional": false,
"cloneObjects": false,
"cloneMethod": "parse-stringify",
"asyncListeners": false,
"disableChangesApi": true,
"disableDeltaChangesApi": true,
"autoupdate": false,
"serializableIndices": true,
"ttl": null,
"maxId": 1,
"DynamicViews": [
],
"events": {
"insert": [
null
],
"update": [
null
],
"pre-insert": [
],
"pre-update": [
],
"close": [
],
"flushbuffer": [
],
"error": [
],
"delete": [
null
],
"warning": [
null
]
},
"changes": [
]
}
],
"databaseVersion": 1.5,
"engineVersion": 1.5,
"autosave": false,
"autosaveInterval": 5000,
"autosaveHandle": null,
"throttledSaves": true,
"options": {
"persistenceMethod": "fs",
"autoload": true,
"serializationMethod": "normal",
"destructureDelimiter": "$<\n",
"recursiveWait": true,
"recursiveWaitLimit": false,
"recursiveWaitLimitDuration": 2000,
"started": 1511382869926
},
"persistenceMethod": "fs",
"persistenceAdapter": null,
"verbose": false,
"events": {
"init": [
null
],
"loaded": [
],
"flushChanges": [
],
"close": [
],
"changes": [
],
"warning": [
]
},
"ENV": "NODEJS"
}
code to find and display record is:
var lastfilecollection = db.getCollection("lastfileplayed");
var entryCount = lastfilecollection.count();
var lastaudio = "";
var nextaudio = "";
if (entryCount==0) {
lastaudio = "none";
nextaudio = files_[0];
} else {
lastaudiorec = lastfilecollection.find({'lastplayed':'correct'});
console.log("1: "+lastaudiorec);
console.log("2: Last file have been played is : " + lastaudiorec.name);
lastaudio = lastaudiorec.name;
nextaudio = getNextFileToPlay(lastaudiorec.name);
}
the console output for log 1:
[ { name: '00001.wav',
lastplayed: 'correct',
meta:
{ revision: 6,
created: 1511381542440,
version: 0,
updated: 1511382847156 },
'$loki': 1 } ]
the console output for log 2:
Last file have been played is : undefined
UPDATE: I have managed to get his working using get instead of find, if someone can still help me with getting it to work with find that would be great. thank you
find() is returning an array as your first console log shows... you can either access via lastaudiorec[0].name or use the findOne() method if you expect single match.
thank you @obeliskos for your help, i will now close my stupid question :D
hah.. no worries 😊