1

我仔细检查了 mongoose 的 API 文档,但我没有找到解决我的问题的线索。我想使用推送功能将新孩子添加到标签孩子。孩子具有与父母相同的结构。

这不适用:

function get_directory(id, cb) {
    // if argument id is omited, we return directly the mainDir

    model.findOne(...select the main object, function(mainDir) {
        recurse_in_the_main_object(mainDir, id)
        callback ( dir_founded )
    });
};

function add_directory(parentDir_id, specs, cb) {
    get_directory(parentDir_id, function(dir) {
        dir.push({..........});

        get_directory(function(mainDir) {
           mainDir.save();
        });
    });
});

但与之合作:

function get_directory(id, cb) {
    // if argument id is omited, we return directly the mainDir

    model.findOne(...select the main object, function(mainDir) {
        recurse_in_the_main_object(mainDir, id)
        callback ( dir_founded, mainDir)
    });
};

function add_directory(parentDir_id, specs, cb) {
    get_directory(parentDir_id, function(dir, mainDir) {
        dir.push({..........});              
        mainDir.save();
    });          
});

这可能是一个异步问题(新目录被很好地推送但没有保存)但即使使用 cb 或 setTimeout,它仍然存在!我只是知道为什么 ex1 不起作用...对不起我的英语^^'

谢谢 !!!

4

0 回答 0