So I'm trying to learn a little NodeJS and working with mongoDB. It's been pretty fun so far but I'd need help with that next step: I'm trying to insertOne into the collection from an async function. Since the function is async it appears that the insert completes before the info it has to insert comes back.
Anyways, I'm trying to insert an object that I got from a function basically and can't seem to do it correctly, can anyone point me into the right direction?
Here's the github so that you can get the npm modules I'm using and here's the code:
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
const KrakenClient = require('@warren-bank/node-kraken-api')
const kraken = new KrakenClient('api_key', 'api_secret', {timeout: 10000})
var url = 'mongodb://localhost:27017/mongoTuto';
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected to the server.");
insertDocuments(db, function() {
db.close();
});
});
var insertDocuments = function(db, callback) {
var collection = db.collection('documents');
collection.insertOne({
ticker
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted documents into the collection");
callback(result);
});
}
var ticker = kraken.api('Ticker', {"pair": 'XXBTZEUR'})
.then((result) => {
result
})
.catch((error) => {
console.log('Error:', error.message)
});
Thanks in advance for your help and sorry for that very noobish question!