小编Ris*_*vik的帖子

修改mongoose对象文字结果不起作用

可能重复:
为什么不能修改Mongoose Query返回的数据(例如:findById)

首先,我正在对mongoDB进行查询,得到所有正确的结果,但只对对象文字的小修改不起作用.我想做的是为评论添加新字段.我尝试使用DBref方法,但它没有工作,所以我现在进行2次查询.

var query = Rss.findOne({ _id: itemId});
query.exec(function(err, feed) {
  if (!err && feed.comments) {
    console.log(feed.comments.length);
    for (var index in feed.comments) {
      var author = feed.comments[index].author;
      if (author !== undefined) {
        User.findById(author, function(err, user) {

          /**Problem is here **/
          feed.comments[index].name = 'Some random field';
          console.log('Added new field' + util.inspect(feed));

        });
      }

    }
  }
});
Run Code Online (Sandbox Code Playgroud)

响应是没有缺少.name字段的响应.

Added new field{ _id: 4f34f343c7b0434217000012,
  original_link: 'http://com',
  publish_date: Fri, 10 Feb 2012 10:36:00 GMT,
  summary: 'some text',
  title: 'title exampel',
  comments: 
   [ …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose node.js

5
推荐指数
1
解决办法
2867
查看次数

Service Worker:如何在服务器上更改文件时更新缓存?

你使用什么缓存策略?我阅读了脱机食谱,最简单的策略是缓存静态内容,遗漏API调用.

这个策略看起来像这样:

  1. 检查请求是否已在缓存中
  2. 如果没有将请求,响应对添加到缓存
  3. 回复

如果在服务器端文件已更改,如何更新缓存?目前,客户端始终获得缓存结果.

这是我的缓存策略的代码:

// You will need this polyfill, at least on Chrome 41 and older.
importScripts("serviceworker-cache-polyfill.js");

var VERSION = 1;

var CACHES = {
    common: "common-cache" + VERSION
};

// an array of file locations we want to cache
var filesToCache = [
    "font-cache.html",
    "script.js",
];

var neededFiles = [
    "index.html"
];

var errorResponse = function() {

    return new Response([
            "<h2>Failed to get file</h2>",
            "<p>Could not retrive response from cache</p>"
        ].join("\n"),
        500
    );
}; …
Run Code Online (Sandbox Code Playgroud)

javascript browser-cache service-worker

5
推荐指数
1
解决办法
3840
查看次数

5
推荐指数
1
解决办法
1038
查看次数

高地中的请求流未正确传递

我在使用nodejs request流时遇到问题,因为我传递给它已经是空的数组.

var _       = require('highland'),
    fs      = require('fs'),
    request = require('request');

// This works but not using the stream approach
// function get(path) {

//     return _(function (push, next) {

//         request(path, function (error, response, body) {
//             // The response itself also contains the body
//             push(error, response);
//             push(null, _.nil);
//         });
//     });
// }

var google = _(request.get('http://www.google.com'));

google
// res is empty array
.map(function (res) {
    // console.log(res);
    return res;
})
// res …
Run Code Online (Sandbox Code Playgroud)

stream node.js highland.js

2
推荐指数
1
解决办法
765
查看次数