繁体   English   中英

node.js,测试mongodb的保存和加载

[英]node.js, testing a mongodb save and load

也许我只是在弄清楚回调性方面遇到麻烦,但是我无法找到一种测试在node.js中进行保存和加载的方法。

我的测试是这样的:

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);

经过测试:

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);
};

有没有一种方法可以将回调传递给saveItems ,以便在完成所有 mongo保存之前不调用getItems

尝试这个 :)

vows.describe('Saving').addBatch({
    'Single item can be saved':{
        topic:function () {
          var topicThis = this;
          // NEW: having a callback here
          myStore.saveItems(1, [{id:3,name:'squat'}], function(err, results){
              myStore.getItems(1, topicThis.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);

// https://github.com/caolan/async
var async = require('async');
// NEW: having a callback here
exports.saveItems = function (deviceId, items, cb) {
    var itemsCollection = db.collection('items');

    itemsCollection.find({deviceId:deviceId}).toArray(function (err, existingItems) {
        var tasks = [];
        // here you are iterating over each item, doing some work, and then conditionally doing an async op
        _.each(items, function (item) {
            item['deviceId'] = deviceId;
            var existingItem = _.find(existingItems, function (existingItem) {
                return existingItem.id === item.id
            });
            if (typeof(existingItem) === 'undefined') {
                // so this is async, b/c it's talking to mongo
                // NEW: add it to our list of tasks, when are later run in parallel
                tasks.push(function(nextTask){
                    itemsCollection.save(item, nextTask);
                });
            }
        });
        // NEW: run it all in parrallel, when done, call back
        async.parallel(tasks, function(err, results) {
            cb(err, results);
        })
    });
};

使用node.js mongo db驱动程序: http : //mongodb.github.com/node-mongodb-native/api-articles/nodekoarticle1.html

插入和更新功能均由回调定义。 我只使用update函数,因为它也可以用作插入(如果下面的第一个参数未获取任何记录)。

itemsCollection.update({_id: itemId}, item, {upsert:true, safe:true}, function (err, result) {
  // callback here
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM