Promise.all不用于asynchronous内部函数

我有一个项目(菜),我需要从MongoDB的每个项目(盘)的业主清单。 我正在使用承诺执行这样的 Dish.find({}) .limit(lim) .sort({created : 1}) .exec() .then(function(dishes){ if (!dishes) { return next(new errors.ResourceNotFoundError('The resource you requested could not be found.')) next() } return dishOwners.getOwners(dishes) }) .then(function(data){ return res.send(200, data) next() }) .catch(function(err){ return next(new errors.InternalError(err.message)) next() }) dishOwners.getOwners(盘)函数返回一个Promise.all,它像所有的盘子一样 module.exports.getOwners = function(dishes){ return Promise.all(dishes.map(function(dish){ User .findOne({_id:dish.created_by}) .then(function(user){ return { dish: dish, owner: user } […]

在Node.JS中一次读取大量文件N行

我有一个65,000,000行的文件,大小约2GB。 我想一次读取N行这个文件,执行db插入操作,然后读取下一个N,在这种情况下N是1000。 插入顺序并不重要,所以同步是好的。 这样做的最好方法是什么? 我只发现一次加载1行,或者将整个文件读入内存的方法。 下面的示例代码,我一直用它读取一行文件。 : var singleFileParser = (file, insertIntoDB) => { var lr = new LineByLineReader(file); lr.on('error', function(err) { // 'err' contains error object console.error(err); console.error("Error reading file!"); }); lr.on('line', function(line) { insertIntoDB(line); // 'line' contains the current line without the trailing newline character. }); lr.on('end', function() { // All lines are […]

Mongoose findByIdAndUpdate没有更新数据

似乎无法看到为什么findByIdAndUpdate没有更新数据。 我改变它findByIdAndRemove,看看我能否发现错误,但当我改变它,它确实删除,所以我不能追查到这个问题。 提前感谢您的帮助。 这是路线 router.get("/:id/edit", function(req, res){ Product.findById(req.params.id, function(err, foundProduct){ res.render("admin/edit",{product: foundProduct}); }); }); router.put("/:id",function(req, res){ Product.findByIdAndUpdate(req.params.id, req.body.product, function(err, updatedProduct){ if(err){ res.redirect("/"); }else{ res.redirect("/admin/inventory"); } }); }); 这里是button来编辑表单: <h2>This is the inventory page!</h2> <% include ../partials/header%> <a href="/admin/new">Add a new product</a> <div class = "row"> <% products.forEach(function(product){ %> <div class = "col-sm-6 col-md-4"> <div class […]

访问对象中数据的复杂性

在我作为日常工作的一部分的一些项目中,我需要访问非常大的JS对象中的数据(数以千计的键值对)。 我试图提高我的代码的效率,所以我想出了几个问题: 当访问这样一个对象中的字段时,JS的运行时复杂度是多less? 我最初的预感是O(n) 通过点符号或括号表示访问时是否有区别? (例如obj.field vs obj[field] ) 我猜测对于不同的运行时引擎有不同的答案 – 有没有一个地方我可以看到它们之间的区别?

如何安装和使用Jade

最近我在YouTube上看了一些video教程,碰到这个JADE的东西,我怎么安装和使用它来进行我的网页开发? 我search了一下,但没有find一个适当的网站,教一步一步继续。 而网站jade-lang.com不可用。 我从网站做到这一点,但node.js抛出一些错误。 下面的截图:

Node.js群集工作者:closures事件

考虑以下的node.js集群configuration。 如何offcallback以防止进一步的消息callback? 碰巧没有off方法。 我必须更新一个新的callback,看来所有的旧callback也被触发。 cluster.on('fork', worker => { worker.on('message', msg => {// Do something…}) })

MongoDB $ cmp(聚合)给出错误的结果

我正在使用mongo的cmp聚合,并得到它的工作,但我认为我遇到的问题是一个错误。 当我比较10(或更大)说8.3时,返回-1。 当我比较9.5到8.3时,返回1。 好像我把十分之一或以上的数字与一个数字相比,我得到一个-1。 如果我比较一个人的工作正常。 以下是比较和结果的部分 cmpPrice: { $cmp: ['$amount', '$ticker.price_usd'] }, case "price": if (doc.cmpPrice == -1 && doc.direction == "above") { console.log("\t\tConditions Met, Price Above" ); emailUser(doc); } else if (doc.cmpPrice == 1 && doc.direction == "below") { console.log("\t\tConditions Met, Price Below"); emailUser(doc); } else { console.log("\t\tFailed"); } break; 结果 – ID: 59766722b006792254355adb […]

对于React组件,<img>中的.jpg模块parsing失败?

我想在我的网站上显示一个图像,我正在使用反应。 这里是组件源代码: import React, { Component } from 'react'; import style from './styles/AboutMeStyle.css'; export default class AboutMe extends Component{ render(){ return ( <div> <img src={require ('./images/ProfilePic.jpg')}align="middle"/> </div> ); } } 该文件似乎被发现,但我不断收到一个exception,说明模块parsing失败…? 这里是来自terminal的错误的文本输出: Module parse failed: /Users/megan/Desktop/Personal Website/client/components/images/ProfilePic.jpg Unexpected character (1:0) You may need an appropriate loader to handle this file type. SyntaxError: Unexpected character (1:0) […]

在module.exports中新增的目的是什么?

我有一个testing分为3个部分。 主页面对象页面,页面有问题的HomePage和testing本身。 我想要HomePage来扩展Page对象。 但是我发现的是 – 如果在页面类中我使用: module.exports = new Page() 我得到错误ERROR: Class extends value #<Page> is not a function or null 但是,如果我使用 module.exports = Page 它完美的作品。 有人可以解释为什么这样吗? 这是因为页面对象本身只是与每个页面的父页面,例如HomePage或LoginPage或任何其他页面构造? class Page { constructor() { this.title = 'My Page'; } open(path) { browser.url('/' + path); } } module.exports = Page; == var Page = require('../page-models/Page'); class HomePage […]

Docker图像与基础图像混合

我想构build一个使用NodeJs和Java Runtime的docker镜像。 有可能find不同的docker文件,每个基地一个。 我如何使用两个基地来创build一个新的图像? 在这种情况下,我要运行一个angular度的应用程序,并在同一图像的春季应用程序。 有关如何拥有这样的要求的单个图像的任何提示? 是否有意义?