Behebung von NodeJS MongoDB 'Cannot read property 'high_' of null'
English
Deutsch
Wenn du eine Fehlermeldung wie diese erhältst
nodejs_mongo_traceback.txt
TypeError: Cannot read property 'high_' of null
at Long.equals (/home/uli/dev/NMUN/node_modules/bson/lib/bson/long.js:236:31)
at nextFunction (/home/uli/dev/NMUN/node_modules/mongodb-core/lib/cursor.js:473:16)
at Cursor.next (/home/uli/dev/NMUN/node_modules/mongodb-core/lib/cursor.js:763:3)
at Cursor._next (/home/uli/dev/NMUN/node_modules/mongodb/lib/cursor.js:211:36)
at nextObject (/home/uli/dev/NMUN/node_modules/mongodb/lib/operations/cursor_ops.js:192:10)
at hasNext (/home/uli/dev/NMUN/node_modules/mongodb/lib/operations/cursor_ops.js:135:3)
(...)hast du wahrscheinlich Code wie diesen:
cursor_example.js
const cursor = db.getCollection('mycollection').find({})
while (cursor.hasNext()) {
const doc = cursor.next();
// ... doc verarbeiten ...
}Die Lösung ist ganz einfach: Da find(), cursor.hasNext() und cursor.next() alle Promises zurückgeben, kannst du ihre Ergebnisse nicht direkt verwenden.
Dieses Beispiel zeigt dir, wie es richtig mit async/await funktioniert:
cursor_example_async.js
const cursor = await db.getCollection('mycollection').find({})
while (await cursor.hasNext()) {
const doc = await cursor.next();
// ... doc verarbeiten ...
}Denke daran, dass die Funktion, die diesen Code enthält, eine async-Funktion sein muss. Siehe die Mozilla-Dokumentation oder google nach Javascript async tutorial, um mehr über die Details zu erfahren!
Check out similar posts by category:
Databases, Javascript
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow