简体   繁体   English

Node.js承诺:获取并保存结果

[英]Node.js Promises: get & save results

I'm new to Node.js promises, I want to do something pretty basic: retrieve "searches" entries in the history db, retrieve "pages" entries in the history, then display the both. 我是Node.js的新手,我想做一些非常基本的事情:在历史记录数据库中检索“搜索”条目,在历史记录中检索“页面”条目,然后同时显示两者。

I've written this piece of code but it doesn't work. 我已经编写了这段代码,但是没有用。 The second get_entries_of doesn't work because the final console.log is called before it has enough time to call the db. 第二个get_entries_of不起作用,因为在没有足够的时间调用数据库之前调用了最终的console.log Can you help me? 你能帮助我吗?

var history = {};

var savePage = function(obj) 
{
    history.page = obj;
}

var saveSearch = function(obj) 
{
    history.search = obj;
}

var displayHistory = function()
{
    console.log(history);
}

/** Promises chain */
historyLib.get_entries_of(uid, "page")
.then(function(obj) {
    savePage(obj);
    historyLib.get_entries_of(uid, "search");
}, console.log)
.then(function(obj) {
    saveSearch(obj);
    displayHistory();
}, console.log);

get_entries_of: get_entries_of:

function get_entries_of(uid, type, date_start, date_end)
{
     var deferred = Q.defer();

var extra = "";
if(typeof(date_start) !== 'undefined')
    extra = ' ,"date": {"$gte": ' +date_start+ ', "$lt": ' +date_end+ '}';

history[type].find({ _uid: mongoose.Types.ObjectId(uid) + extra}, deferred.makeNodeResolver());

return deferred.promise;
}

Without more details, it looks like your first promise callback doesn't return a promise. 如果没有更多详细信息,则您的第一个promise回调似乎不会返回promise。 So that first search probably needs to look like this: 因此,第一次搜索可能需要如下所示:

historyLib.get_entries_of(uid, "page")
.then(function(obj) {
    savePage(obj);
    return historyLib.get_entries_of(uid, "search");
}, console.log)
.then(/* go on here with the second search */)`

So once again: return historyLib.get_entries_of(uid, "search"); 再次这样: return historyLib.get_entries_of(uid, "search");

Note we return a promise here? 请注意,我们在这里退还诺言吗? Actually we return whatever get_entries_of returns which is a promise. 实际上,我们返回的是get_entries_of所承诺的返回值。 It means that this whole callback (function(obj){}) will also return a promise and whatever that function returns will be appended on the promise chain. 这意味着整个回调(function(obj){})也将返回一个Promise,并且该函数返回的任何内容都将附加在Promise链上。

That's why your second promise will also work. 这就是为什么您的第二个承诺也将起作用的原因。 Otherwise it would look like your first then(cb) callback (the cb thing) returns undefined, and you cannot call then() of undefined. 否则,它看起来像您的第一个then(cb)回调( cb事物)返回undefined,并且您无法调用undefined的then()

What does your console.log say, anyway? 无论如何,您的console.log说什么?

Also on a side note, are you sure that this query works when you pass it the start_date too? 另外,请注意,当您也将start_date传递给该查询时,您确定该查询有效吗? This: { _uid: mongoose.Types.ObjectId(uid) + extra} doesn't look correct, but I might be wrong. 这: { _uid: mongoose.Types.ObjectId(uid) + extra}{ _uid: mongoose.Types.ObjectId(uid) + extra}看起来不正确,但我可能是错的。 To me it looks like you want: 在我看来,您似乎想要:

var criteria = {_uid: uid};
if(date_start) {
    criteria.date = {
        $gte: date_start
    };// etc
}
stuff.find(criteria, callback); 

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

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