【问题标题】:Multiple errors when trying to save to DB using mongoose and async尝试使用猫鼬和异步保存到数据库时出现多个错误
【发布时间】:2016-08-04 09:49:36
【问题描述】:

我正在尝试使用 mongoose 将某些内容保存到数据库中。问题是我需要确保在我继续程序并关闭连接之前完成保存。知道保存在猫鼬中是异步的,我尝试使用此代码:

saveFunction = function(song){
song.save(function(err, userObj){
    if(err){
        console.log('Error' + err);
    } else{
        console.log('saved successfully:', userObj);
    }

});
};

database.prototype.add= function(newSong){
mongoose.connect(url);

var song = new songModel({id : newSong.getId(),
    title : newSong.getTitle(),
    artist : newSong.getArtist,
    genre : newSong.getGenre(),
    rating : newSong.getRating(),
    link : newSong.getLink()});


console.log("before async");
async.parallel([function (callback){
    saveFunction(song);
    callback();
}],function(){
    mongoose.connection.close();
    console.log('closed connection');
});
console.log("after async");
nextFreeId++;
};

^songModel 是全局定义的。

我尝试了很多不同的方法并改变了很多东西,但我总是遇到某种错误。使用此代码,我得到一个 process.nexttick(function() throw err ) 错误。我只是无法让它工作。有人可以告诉我有什么问题或为我提供工作代码吗?

我认为最好的控制台应该是这样的:

before async
saved successfully
closed connection
after async

谢谢!

编辑:也可以使用异步的其他替代方案。我只想让这段代码以任何可能的方式工作。我只需要保存/查找/删除某些内容,它需要等待程序执行的其余部分,直到保存/查找/删除完成。我变得非常绝望,一个人在这个问题上浪费了将近一天的时间:(

【问题讨论】:

  • 你现在得到什么输出?
  • 您可能不希望每次调用此函数时都打开和关闭 Mongoose 连接池。只需在启动期间连接并保持打开状态。
  • @ayushgp 我在 async 之前得到 afer async 然后成功保存然后连接关闭。

标签: javascript node.js mongodb asynchronous mongoose


【解决方案1】:

你需要从你的保存函数中返回一个回调。

saveFunction = function(song,callback){
    song.save(function(err, userObj){
        if(err){
            console.log('Error' + err);
            return callback(true,err)
        } else{
            console.log('saved successfully:', userObj);
            return callback(null);
        }
    });
};

编辑

根据您的评论,您所期望的行为将永远不会发生。你期待

console.log("before async");

async.parallel -> do your bits 
console.log('closed connection');

console.log("after async");

但是这永远不会发生,因为async.parallel 是一个异步调用,这意味着执行不会等待在执行下一个命令之前完成。您看到的行为是

console.log("before async");

async.parallel -> starts

console.log("after async");

async.parallel -> console.log('closed connection');

节点正在做第一个日志,开始async.parallel,然后是“异步后”的console.logging。然后当async.parallel 到达它的回调函数时,它会打印“关闭的连接”,所以它出现在“异步之后”之后,因为它是在之后执行的。

您要执行的任何依赖于async.parallel 结果的逻辑必须在回调函数中发生。此外,async.parallel 用于异步运行 2 个或更多函数,然后在它们全部完成后执行回调。您的解决方案不需要 async.parallel。您可以将其替换为:

saveFunction(song,function(err){
    if(err){
        //failed to save song
    }
    mongoose.connection.close(); //you do not need to do this anyway
    console.log('closed connection');
    nextFreeId++;
    //anything else you need to do here
});

【讨论】:

  • 谢谢你,在你的帮助下让它工作。非常喜欢编辑:实际上我认为它有效,但在“ater async”之后我仍然完成了保存操作;有什么想法吗?
  • @user3764582 将console.log("after async") 和nextFreeId++; 移动到回调函数中。见编辑。
  • 这并没有改变我的想法。当我在并行调用后执行代码时,我仍然无法确定保存是否完成。 (这不是我测试过的)。在继续执行我的程序之前,我需要确保保存已完成并且连接已关闭。目前它不这样做:s 谢谢顺便说一句!
  • 我明白你现在的意思了。在这种情况下,我将不得不重新考虑我的程序结构。感谢您清理一切。
猜你喜欢
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2012-02-02
相关资源
最近更新 更多