Tag: ecmascript 6

JavaScriptlogging对象而不在\ n上执行换行符

我有一个对象: class MyObject{ constructor(prop_a, prop_b, prop_c) { this.property_a = property_a; this.property_b = property_b; this.property_c = property_c; } const myObject = new MyObject(…data); 它在日志输出中看起来像这样: myObject { property_a:0001, property_b:somestringsandlettersthat\n havenewlines\n andmorenwelines\n andmorenwelines, property_c:someotherstuff } 但我想要这样的: myObject { property_a:0001, property_b:somestringsandlettersthat havenewlines andmorenwelines, andmorenwelines property_c:someotherstuff } 新行也应该在\ n不仅在新的属性上。 我使用的logging器是: https : //github.com/VeliovGroup/Meteor-logger 在console.log(myObject)中看起来更糟糕: myObject { property_a:0001, property_b:somestringsandlettersthat\n havenewlines\n andmorenwelines\n […]

茉莉花function导出没有父对象,并在NODE中使用ES6导入

经过大量的研究,我找不到一种方法来模拟不带父对象的导出函数。 例如我试图模拟函数导出以下方式: module.exports = function thing(event) {}; 或在ES6 export function thing(event) {}; 在将这些文件导入到testing文件中时,我尝试像这样导入: import {thing} from 'emvio-util-responses; //call some function that calls thing() spyOn(???, 'thing').and.returnValue({}); expect(???.thing).toHaveBeenCalled(); 我已经尝试了很多方法来完成这一点,但模拟不被称为。 有些人build议导入*并提供一个别名作为父对象。 喜欢这个: import * as SomeObj from 'emvio-util-responses; //call some function that calls thing() spyOn(SomeObj , 'thing').and.returnValue({}); expect(SomeObj .thing).toHaveBeenCalled(); 这不起作用。 其他人则build议使用窗口对象 spyOn(window, 'thing').and.returnValue({}); 但是我在节点:(。

为什么nodejs在课堂上使用asynchronous时给我错误?

class Controller { async getUser(username) { return User.find({username}); } } module.exports = new Controller(); getUser是意外标识符 为什么,它在巴贝尔工作良好。

根据更新的字段调用函数

我想调用不同的function取决于更新的模型领域。 我的代码如下所示: update(req, res){ return LED .findById(req.params.LEDId) .then(LED => { if (!LED) { return res.status(400).send({ message: 'LED Not Found', }); } return LED .update(req.body, {fields: Object.keys(req.body)}) .then(() => res.status(200).send(LED)) .catch((error) => res.status(400).send(error)); }) .catch((error) => res.status(400).send(error)); }, 所以我的计划是集成一些if-clause来获取更改的值,并根据更改调用一些函数。 如果从句: if(req.body.status || LED.status){ BLE.changeStatus(req.body.device_ID,req.body.status); }else if(req.body.prog || LED.prog){ BLE.changeProg(req.body.device_ID,req.body.prog); }else if(req.body.white || LED.white){ BLE.changeWhite(req.body.device_ID,req.body.white); }else if(req.body.color […]

node.js | 从其他文件调用类实例的函数

我有一个bot.js类,其中我做了getUserInventory函数。 我在我的主文件app.js中需要bot.js并创build了一个新的实例。 bot.js class SteamBot { constructor(logOnOptions){ […] this.logOn(logOnOptions); } logOn(logOnOptions){ […] } getUserInventory(sid, gameid, contextid, onlyTradeable, callback){ […] } } module.exports = SteamBot; app.js const SteamBot = require('./bots/bot.js'); const bot = new SteamBot({ 'logOnOptions' }); 现在我只需键入bot.getUserInventory(…)就可以在我的app.js文件中调用函数getUserInventory 但是如果我想在另一个文件中调用该函数呢? 我必须input与我在app.js中编写的代码相同的代码吗? 或者会导致问题? 任何答复表示赞赏。 而且我知道这是一个不好的问题,但请不要因为这个问题而投下我的问题

节点套接字应用程序新实例作用

这是一个简单的套接字应用程序使用事件库模式 const invitation = require('./invitation'); module.exports = function(io){ io.on('connection', (socket)=>{ var prepareGame = new PrepareGame(socket) socket.on("sendInvitation",(data, ack)=>{ prepareGame.sendInvitation(data,ack) }); }); } 并在prepareGame.js const events = require('events'); const util = require('util'); class PrepareGame extends events { constructor(socket) { super(); this.user = socket.user var self = this draftInvitation(data){ this.newInvitation = { from_user: self.user.id, to_user: data.to_user, message:data.message, created_at:moment().unix(), […]

NodeJS / Javascript – 使用承诺来处理图像收集,然后保存汇总的结果

股票溢价 – 我试图用NodeJS处理图像集合(〜2000图像)。 我能够提取所需的信息,但我很难得到正确的时间,以便将结果保存到JSON文件。 接近尾声,你会看到 console.log(palette); // Push single image data to output array. output.push(palette); console.log工作正常,但是在将空的output数组写入data.json之后,似乎执行了push方法。 试过有一个嵌套的承诺,我不会写文件,直到所有的图像已被处理。 getPixels中的callback函数是asynchronous执行的。 输出数组的顺序是任意的。 任何和所有帮助非常感谢! 谢谢! // Extract color information from all images in imageDirectory var convert = require('color-convert'), fs = require('fs'), getPixels = require("get-pixels"), startTime = Date.now(); var processedImages = new Promise((resolve, reject) => { var imageDirectory = 'input', […]

什么是JavaScript中的“导出默认”?

文件: SafeString.js // Build out our basic SafeString type function SafeString(string) { this.string = string; } SafeString.prototype.toString = function() { return "" + this.string; }; export default SafeString; 我从来没有见过export default 。 是否有任何相当的东西export default ,可以更容易理解?

javascript:ReferenceError:导出未定义

es6 / Babel:我的文件夹看起来像这样: A/ index.js a.js …other 我通过index.js文件公开a.js内容如下: export { foo, bar } from './a'; 这样我就可以使用以下方式导入它: import { foo, bar } from '../A'; 但是现在,我想在index.js使用* ,以便万一从a.js导出另一个东西,它会自动显示出来: export * from './a'; 但是当我这样做,我得到这个错误: Uncaught ReferenceError:未定义导出 我在这里做错了什么?

Cryptographer TypeError:第一个参数必须是一个string

所以我想创build一个pincode /密码散列的unit testing。 我之前遇到过这个错误,但是后来我用一个简单的.toString()函数解决了这个错误,但是现在看起来并不奏效。 这是我的一些代码相关的方法: pin.model.spec.js: let pin = Math.floor(1000 + Math.random() * 9000); let hash = await Pincode.getHashedPincode(pin.toString(), salt); crypto.js: function getPasswordHash(password, salt) { return new Promise((resolve, reject) => { crypto.pbkdf2(password, new Buffer(salt, 'hex'), iterations, keyLength, 'SHA1', (err, result) => { if (err) { return reject(err); } return resolve(result.toString('hex')); }); }); } 不要担心不一致的方法名称,它们之间有一个名为getHashedPincode的方法,除了调用getPasswordHash命名约定外, variablesiterations和keyLength来自一个常量文件。 […]