Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js mongoose查找结果不刷新_Node.js_Mongodb_Express_Mongoose_Pug - Fatal编程技术网

Node.js mongoose查找结果不刷新

Node.js mongoose查找结果不刷新,node.js,mongodb,express,mongoose,pug,Node.js,Mongodb,Express,Mongoose,Pug,我正在使用express/jade/mongodb创建一个带有db的站点(这是一个非常新的站点)。 在使用此函数获取方法期间,我正在使用“mongoose find”从数据库检索列表: function getBookList(Book, req, callback){ Book.find({'username': req.user.username}, 'bookTitle author', function(err, userBooks) { if (err) return ha

我正在使用express/jade/mongodb创建一个带有db的站点(这是一个非常新的站点)。 在使用此函数获取方法期间,我正在使用“mongoose find”从数据库检索列表:

function getBookList(Book, req, callback){
  Book.find({'username': req.user.username}, 'bookTitle author', function(err, userBooks) {
    if (err) return handleError(err);
    else if (userBooks.length > 0) {
      bookList.push(userBooks);
      callback();
    }
  });
};

router.get('/', ensureAuthenticated, function(req, res, next) {
 getBookList(Book, req, function(){
      res.locals.user.books = bookList[0];
      res.render('index', { title: 'library' });
  });
});
在我的jade文件中,代码是:

ul
    each book in user.books
        li #{book.bookTitle}
            span  -  
            span #{book.author}
第一次与用户登录时,我得到了预期的列表,但如果我向数据库添加文档并再次呈现页面,页面上的列表将不会更新并保持原样。 即使在一次注销和另一次登录之后,它仍然保持不变。只有在重新启动服务器后,列表才会更新。
有人能解释一下我做错了什么吗?

每次调用
getBookList
,都会将生成的书籍数组推到另一个数组中,
bookList

假设数据库中有一个文档,请调用
getBookList
。之后,
bookList
将如下所示:

bookList = [ [ 'book 1' ] ]
bookList = [ [ 'book 1' ], [ 'book 1', 'book 2' ] ]
function getBookList(username, callback){
  Book.find({'username': username}, 'bookTitle author', function(err, userBooks) {
    callback(err, userBooks);
  });
};

router.get('/', ensureAuthenticated, function(req, res, next) {
 getBookList(req.user.username, function(err, userBooks) { 
  if (err) return handleError(err);
  else if (userBooks.length > 0) {
    res.locals.user.books = userBooks;
    res.render('index', { title: 'library' });
  } else {
    // handle the situation where no books were found
    ...
  }
});
然后添加另一本书,并再次调用
getBookList
。现在
bookList
如下所示:

bookList = [ [ 'book 1' ] ]
bookList = [ [ 'book 1' ], [ 'book 1', 'book 2' ] ]
function getBookList(username, callback){
  Book.find({'username': username}, 'bookTitle author', function(err, userBooks) {
    callback(err, userBooks);
  });
};

router.get('/', ensureAuthenticated, function(req, res, next) {
 getBookList(req.user.username, function(err, userBooks) { 
  if (err) return handleError(err);
  else if (userBooks.length > 0) {
    res.locals.user.books = userBooks;
    res.render('index', { title: 'library' });
  } else {
    // handle the situation where no books were found
    ...
  }
});
但是,您只使用了
bookList[0]
,因此第一次调用
getBookList
的结果是。这永远不会包含新文档,因为这些文档只会出现在
图书列表
的后续条目中

但这并不是要解决的最大问题,因为您使用
bookList
作为全局变量,这不是一个好主意。相反,
getBookList
应该将书籍列表传递回调用者

这将使代码看起来像这样:

bookList = [ [ 'book 1' ] ]
bookList = [ [ 'book 1' ], [ 'book 1', 'book 2' ] ]
function getBookList(username, callback){
  Book.find({'username': username}, 'bookTitle author', function(err, userBooks) {
    callback(err, userBooks);
  });
};

router.get('/', ensureAuthenticated, function(req, res, next) {
 getBookList(req.user.username, function(err, userBooks) { 
  if (err) return handleError(err);
  else if (userBooks.length > 0) {
    res.locals.user.books = userBooks;
    res.render('index', { title: 'library' });
  } else {
    // handle the situation where no books were found
    ...
  }
});

还有一些其他更改,如从模型(
Book
)和请求(
req
)中解耦
getBookList

对于
getBookList
,每次调用都会将生成的图书数组推到另一个数组中,
bookList

假设数据库中有一个文档,请调用
getBookList
。之后,
bookList
将如下所示:

bookList = [ [ 'book 1' ] ]
bookList = [ [ 'book 1' ], [ 'book 1', 'book 2' ] ]
function getBookList(username, callback){
  Book.find({'username': username}, 'bookTitle author', function(err, userBooks) {
    callback(err, userBooks);
  });
};

router.get('/', ensureAuthenticated, function(req, res, next) {
 getBookList(req.user.username, function(err, userBooks) { 
  if (err) return handleError(err);
  else if (userBooks.length > 0) {
    res.locals.user.books = userBooks;
    res.render('index', { title: 'library' });
  } else {
    // handle the situation where no books were found
    ...
  }
});
然后添加另一本书,并再次调用
getBookList
。现在
bookList
如下所示:

bookList = [ [ 'book 1' ] ]
bookList = [ [ 'book 1' ], [ 'book 1', 'book 2' ] ]
function getBookList(username, callback){
  Book.find({'username': username}, 'bookTitle author', function(err, userBooks) {
    callback(err, userBooks);
  });
};

router.get('/', ensureAuthenticated, function(req, res, next) {
 getBookList(req.user.username, function(err, userBooks) { 
  if (err) return handleError(err);
  else if (userBooks.length > 0) {
    res.locals.user.books = userBooks;
    res.render('index', { title: 'library' });
  } else {
    // handle the situation where no books were found
    ...
  }
});
但是,您只使用了
bookList[0]
,因此第一次调用
getBookList
的结果是。这永远不会包含新文档,因为这些文档只会出现在
图书列表
的后续条目中

但这并不是要解决的最大问题,因为您使用
bookList
作为全局变量,这不是一个好主意。相反,
getBookList
应该将书籍列表传递回调用者

这将使代码看起来像这样:

bookList = [ [ 'book 1' ] ]
bookList = [ [ 'book 1' ], [ 'book 1', 'book 2' ] ]
function getBookList(username, callback){
  Book.find({'username': username}, 'bookTitle author', function(err, userBooks) {
    callback(err, userBooks);
  });
};

router.get('/', ensureAuthenticated, function(req, res, next) {
 getBookList(req.user.username, function(err, userBooks) { 
  if (err) return handleError(err);
  else if (userBooks.length > 0) {
    res.locals.user.books = userBooks;
    res.render('index', { title: 'library' });
  } else {
    // handle the situation where no books were found
    ...
  }
});
还有一些其他更改,如从模型(
Book
)和请求(
req
)中解耦
getBookList