Mongoose方法返回undefined

我已经成功地定义和使用我的项目mongoose模型: const Schema = mongoose.Schema; const Item = new Schema({ name : String, hierarchy: [{ type: Schema.ObjectId, ref: this }], hierarchy_size: Number }); 在保存之前和之后,我将相应的hierarchy_size和hierarchy值填充到其属性中,使其成为分层树: Item.pre("save", function(next) { this['hierarchy_size'] = this.hierarchy.length; next(); }) Item.post('save', (doc, next) => { if ((doc._id) && (doc.hierarchy.length == 0)) { doc.hierarchy = [doc._id]; doc.save(); } next(); }) 现在我正在为每个根子节点(在查询hierarchy_size == 1之后)运行以下方法getChildren: Item.methods.getChildren […]

Discord.js – 用户在线或离线时在控制台上发布消息

我一直在试图使用下面的代码,但不知何故,它根本不工作。 这个想法是有一个服务器login控制台上的用户上线和下线。 bot.on("Presence", usr => { if (usr.status == 'offline'){ console.log(`${usr.username} is offline`); } else if (usr.status == 'online') { console.log(`${usr.username} is online`); } });

在一个Cloudant noSQL数据库中search许多参数

我正在使用Bluemix上的Cloudant数据库将产品存储在Node.js服务器中。 这些产品将按类别search。 要查找只有一个类别的产品,将不会成为问题,因为通过将作为search参数发送的string与保存在数据库中的类别string进行比较来进行search。 产品有两个或更多类别时会出现问题。 在进行string与string的比较时,它不会重合。 产品可以有他们需要的种类。 有任何想法吗?

如何模拟服务的用户令牌服务调用

我的应用程序结构是Web应用程序调用一个WebAPI(让我们称之为apiA),并调用另一个API(让我们称之为apiB)。 现在,Web应用程序通过AAD JWT令牌对呼叫进行身份validation。 所以,这个标记将为apiA创build。 但我想api调用apiB作为login用户,并不想使用客户端的秘密等 所以,简而言之,就是要模拟用户,并从为apiA创build的令牌获取apiB的标记,或者可能有一个令牌,这对apiA和apiB都有好处。 所以,我不必得到一个新的令牌。 有没有办法做到这一点?

将json响应中的id映射到key,并将其用于FlatList react-native

我有一个API的JSON响应如下: centres = [{ id: 1, name: "DEF", },{ id: 2, name: "ABC", }] 现在我想在FlatList中填充上面的数据 return( <View> <FlatList data={this.state.centres} renderItem={({item}) => <CentreComponent centre={item}/>} /> </View> ); 但是我不能做以上的数据(中心)没有“关键”财产。 现在我可以遍历数组中的每个项目,并添加一个属性“key”,它具有与ID相同的值。 但我觉得这是有效的。 有没有更好的方法来映射“ID”列“键”来呈现FlatList

如何发布JSON对象angularjs

我想发布对象,并在节点服务器上读取它,但我有一个错误。 控制器: $scope.update = function(contact) { console.log(contact); $http.post('/contactlist/' + contact).then(function(response) { console.log(response); }); }; 服务器节点: app.post('/contactlist/:contact', function (req, res, next) { console.log(req.body); res.json(); }); 服务器节点头: var express = require('express'); var app = express(); var mysql = require('mysql'); var bodyParser = require('body-parser'); var connection = ***************** app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({extended:false})); app.use(express.static(__dirname + "/public")); 错误networkingPOST的屏幕截图: […]

当使用按位运算符时,意味着什么是真实的条件

在js代码的端口上,我在使用按位操作时面临困难。 有一个如果条件,我不知道我完全理解。 我不明白的条件是 if (byteUnderConsideration & Math.pow(2, (7 – bitIndexWithinByte))) return node.right 我不知道在那种情况下什么时候会是真的。 完整的原始代码是, KBucket.prototype._determineNode = function (node, id, bitIndex) { // **NOTE** remember that id is a Buffer and has granularity of // bytes (8 bits), whereas the bitIndex is the _bit_ index (not byte) // id's that are too short are put in […]

用“启动函数”和“参数”调用JavaScript文件

我正在开发一个简单的Electon应用程序。 我有两个JavaScript文件(NodeJs)。 第一个文件是调用第二个..我的代码正在工作… 我想修改这一行: require('./mitm.js'); 接受参数。 我想在mitm.js文件中添加一个启动函数,并在main.js文件中使用object.start(),并使用参数来configuration代理并replace"http://proxy:1111" 例如:obj.start(param) 文件1:main.js const electron = require('electron') const {app, BrowserWindow, session} = require('electron') const path = require('path') const url = require('url') //call mitm proxy require('./mitm.js'); let win function createWindow () { win = new BrowserWindow({width: 800, height: 800}) win.webContents.session.setProxy({proxyRules:'localhost:8080'}, function () { win.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: […]

从node.js中的JSON将s3桶策略转换为base64

我正在尝试在node.js中的base64中对Amazon s3存储桶策略进行编码(更具体地说是StringToSign),但似乎无法获得正确的代码。 我使用由amazon提供的默认策略进行testing,因为它们提供了正确的base64编码。 ( 亚马逊给出的例子 ) 他们的输出是eyAiZXhwaXJhdGlvbiI6ICIyMDE1…….. (保持它很短,因为它很长) 我不断收到eyJleHBpcmF0aW9uIjoiMjAxNS0xMi0zMFQx….. 我的代码来生成这个是: var policyString = JSON.stringify(policy) var policyBytes = utf8.encode(policyString) var stringToSign = base64.encode(policyBytes) 其中policy是以JSON格式保存亚马逊策略的variables,而utf8和base64是节点模块。 这可能听起来像一个新手的问​​题,但我一直盯着它一段时间,任何hlep将不胜感激!

为什么新版本的mocha在运行npmtesting时不能输出testing细节?

我有我的项目指定下列devDependencies : "mocha": "^3.4.2", "mocha-sinon": "^2.0.0", "sinon": "^2.3.7", 当我用这些运行我的testing时,我只得到最小的输出: $ npm test Basic test example. simple test examples 当我将摩卡依赖关系降级到这些版本时: "mocha": "^3.2.0", "mocha-sinon": "^1.1.6", "sinon": "^1.17.7", 我得到了预期的详细输出: $ npm test Basic test example. simple test examples – expects more tests in future. 2 passing (27ms) 1 pending 其他一切都是一样的。 我已经摆弄了一些没有效果的mocha命令行选项。 如何获得新版本的详细输出?