MEAN:无法将数据更新到DB

时间:2016-05-18 02:03:34

标签: javascript mongodb express

我试图通过快递将数据更新为mongodb,虽然我的api响应状态200,我无法将值更新为猫鼬,我使用猫鼬皮肤

scanf("%" #LENGTH "s", str)

我的API

var mongo = require('mongoskin');
var db = mongo.db(config.connectionString, {
    native_parser: true
});
var ObjectId = require('mongodb').ObjectID;
db.bind('users');
db.bind('dashboard')

function update(_id, userParam) {
    var deferred = Q.defer();

    for (var i = 0; i < Object.keys(userParam).length; i++) {
        var set = {

            file: userParam[i].file,
            result: userParam[i].result,
            user_id: userParam[i].user_id

        };
        db.dashboard.update({
                _id: mongo.helper.toObjectID(_id)
            }, {
                $set: set
            },
            function(err, doc) {
                if (err) deferred.reject(err);
                deferred.resolve();
            });
    };
    return deferred.promise;
}

我有一个for循环,所以考虑整个api并推送到mongodb

请同时建议将整个json推送到mongodb的任何替代方案。

非常感谢任何帮助

提前致谢

1 个答案:

答案 0 :(得分:1)

db.dashboard.update是一个异步(a.k.a.非阻塞)函数,它以callback为参数。

您正在db.dashboard.update多次(通过for循环)callback内拒绝或解析相同 promise

这意味着,在调用第一个then的回调时,将执行返回的promise的{​​{1}}方法。

对于其余的更新操作,您的db.dashboard.update功能无法收到通知。

<强>解:

<强> 1

将异步代码放在IIFE内,并在循环的最后一次迭代期间调用then

deferred.resolve

<强> 2

使用async.map。 (这是更优雅的方式)。

function update(_id, userParam) {

    var deferred = Q.defer();

    var length = Object.keys(userParam).length;

    for (var i = 0; i < length; i++) {
        var set = {

            file: userParam[i].file,
            result: userParam[i].result,
            user_id: userParam[i].user_id

        };

        (function(set, i) { // <------------------------- enclosing everything in an IIFE

            db.dashboard.update({
                    _id: mongo.helper.toObjectID(_id)
                }, {
                    $set: set
                },
                function(err, doc) {
                    if (err) {
                        deferred.reject(err);
                    } else if (length - 1 === i) { // <-- checking if this is the last iteration
                        deferred.resolve();
                    }
                });

        })(set, i); // <--------------------------------- sharing set and i with the IIFE scope
    };

    return deferred.promise;
}

此外,function update(_id, userParam) { var deferred = Q.defer(); /** * Array to contain all the * @type {Array} */ var arrayOfSets = []; /** * Converting each key-value pair in userParam into an array element * and pushing it into arrayOfSets */ for (var i in userParam) { arrayOfSets.push({ file: userParam[i].file, result: userParam[i].result, user_id: userParam[i].user_id }); } /** * Executing async.map * * Parameters: * * 1. arrayOfSets - each element will be fed to the function in the 2nd parameter * * 2. function(item, callback) - in this function do the update operation * * 3. function(err, result) - in this function resolve or reject the promise */ async.map(arrayOfSets, function(item, callback) { db.dashboard.update({ _id: mongo.helper.toObjectID(_id) }, { $set: item }, function(err, doc) { if (err) { callback(err, null); } else { callback(null, doc); } }); }, function(err, result) { if (err) { deferred.reject(err); } else { deferred.resolve(result); } }); return deferred.promise; } 代码应始终包含在else

else