Pergunta

Perhaps I'm just having trouble figuring out the callbackyness, but I can't figure out a way to test a save and load in node.js.

My test is this:

vows.describe('Saving').addBatch({
    'Single item can be saved':{
        topic:function () {
            myStore.saveItems(1, [{id:3,name:'squat'}]);
            myStore.getItems(1, this.callback);
        },
        'saved item is returned by getItems':function (err, items) {
            assert.equal(items.length, 1);
            assert.equal(items[0].deviceId, 1);
            assert.equal(items[0].id, 3);
        }
    }
}).export(module);

With this being tested:

exports.saveItems = function (deviceId, items) {
    var itemsCollection = db.collection('items');

    itemsCollection.find({deviceId:deviceId}).toArray(function (err, existingItems) {
        _.each(items, function (item) {
            item['deviceId'] = deviceId;
            var existingItem = _.find(existingItems, function (existingItem) {
                return existingItem.id === item.id
            });
            if (typeof(existingItem) === 'undefined') {
                itemsCollection.save(item);//callback here?
            } else {
            }
        });
    });
};

exports.getItems = function (deviceId, callback) {
    var itemsCollection = db.collection('items');
    itemsCollection.find({deviceId:deviceId}).toArray(callback);
};

Is there a way I can pass a callback to saveItems in such a way that getItems isn't called until all mongo saves are complete?

Nenhuma solução correta

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top