使用mongoose将embedded文档的多个实例保存到我的模型中

我试图在我的模型中保存embedded式文档的多个实例,我期待每当我填写我的表单数据时,embedded式文档的一个新实例被创build并推送到一个数组中。 这是我的预测模式。 const mongoose = require('mongoose'); mongoose.Promise = global.Promise; const slug = require('slugs'); const teamSchema = new mongoose.Schema({ team1: { type: String }, team2: { type: String }, prediction:{ type: String } }); const predictionSchema = new mongoose.Schema({ author:{ type: String }, team: [ teamSchema ] }); module.exports = mongoose.model('Prediction', predictionSchema); 这是我的控制器 const mongoose = […]

创build一个可停止的Linux服务

我试图运行一个NodeJS应用程序作为一个Linux服务。 我在/etc/init.d文件夹下创build了一个服务文件,内容如下: #!/bin/bash # chkconfig: 2345 20 80 # description: Description comes here…. # Source function library. . /etc/init.d/functions start() { # code to start app comes here # example: daemon program_name & /usr/bin/node /home/folder/nodeapp.js & echo "Service started">&2 } stop() { # code to stop app comes here # example: killproc program_name killproc /usr/bin/node […]

同时只运行第一个任务

我正在学习React Native,同时学习打字稿。 我find了一个初始设置的教程,可以一起工作,效果很好。 但是,为了使HRM正常工作,我需要在一个控制台中运行react-native run-android ,并在另一个控制台上npm run watch ,因为这两个任务都控制了控制台。 我下面的教程显示了这个package.json : "scripts": { "start": "node node_modules/react-native/local-cli/cli.js start", "tsc": "tsc", "clean": "rimraf artifacts", "build": "npm run clean && npm run tsc –", "lint": "tslint src/**/*.ts", "watch": "npm run build — -w", "start:android": "npm run build && concurrently -r \"react-native run-android\" \"npm run watch\"" }, 但是,当我运行start:android它只能得到运行react-native run-android后,它永远不会运行npm […]

我如何做一个try / catch为我的特定API调用示例?

所以我的问题是非常具体的。 每当我从我的页面运行这个位,我得到一个错误,如果我不input我需要在API中search正确的ID。 由于查询string不正确,它不知道该做什么,因此无法进行有效的API调用。 如何redirect到不同的页面?如果有这样的错误,或者如何阻止它停止程序? 我假设有一个尝试在这里,但我尝试了多种不同的方式,我仍然困惑,因为它不工作。 请帮助! 我是新来的…这是片段。 如果“bnetID”不是有效的ID,则代码的请求部分是发生错误的地方。 如果它是有效的,它运行得非常好… // Make a GET request to the /results page (When submit is pressed) app.get("/results", function(req, res){ // Retrieve bnetID and REGION from the FORM var bnetID = req.query.bnetID; var region = req.query.region; // Replace the # with a – bnetID = bnetID.replace("#", "-"); // Create the […]

React Native / Node API:在发送之后无法设置标题

我遇到了一个与我的Node API交互的原生应用程序的问题。 当我尝试发布数据通过节点更新数据库中的某些值(然后转到存储过程)时,我得到一个500 – 在Node 中发送后,无法设置标题 。 我能find的所有东西都表示这可能是由于发送了两次响应。 我不认为这是这种情况。 我已经在邮递员testing,事情工作正常,它返回正确的返回数据200的状态。 我正在尝试将数据发布到API,如下所示: const myHeaders = new Headers(); myHeaders.append('Content-Type', 'application/json'); fetch(`http://localhost:3000/user/preferences`, { method: 'POST', headers: myHeaders, mode: 'cors', cache: 'default', body: JSON.stringify({ id: '1', minage: this.state.minageValue, maxage: this.state.maxageValue }) }) .then(response => response.json()) .then(body => console.log(body)) .catch(err => console.log(err)); 我在API端接收数据,并将数据传递给存储过程: function updatePreferences(req, res, next) { console.log(req); var […]

NodeJS日程安排程序:有2或3个工作人员的集群,作业不是均匀分布的

我正在使用伟大的NodeJS议程任务调度程序( https://github.com/agenda/agenda )来安排我的Node / Express应用程序中的后台作业。 议程也可以运行在节点群集configuration,但是我遇到了一个问题(也许我忽略了一些东西,因为这似乎很基本)。 因此,我使用了自述文件( https://github.com/agenda/agenda#spawning-for- ing- processes )中的代码示例来设置一个具有N个工作者的集群,每个工作者(节点集群进程)都有一个议程实例。 现在假设我有两个工人(进程),我从工人1调用“agenda.now()”,然后可以由这两个工人中的任何一个来拾取(处理),因为他们都在监视队列。 但是,我总是看到第一个工人正在接受这个工作,而另一个工作人员却没有接受这个工作。 我忽略了什么? 所有的工作人员都应该监督排队,所以他们都应该找工作。

承诺处理 – 更新数据库条目,如果存在

我坚持对Promises的新挑战。 目标:仅在P_KEY存在的情况下更新数据库条目。 当前的数据库是通过模块公开的,模块已经为db获取和放置方法。 双方都回诺言。 做法: API调用节点js上具有ID和值集的更新方法处理程序(json) 在post方法的处理程序中调用get db模块的方法检查promise成功的值是否为空,如果是则返回false否则为true。 如果属实; 数据存在调用db模块的方法。 但不知何故数据总是返回false。 即使db入口已通过db api。 /** Function to check if p_key exist*/ function checkIfPKExists(idVal) { pkdb.get(idVal).then(function(value) { if(value) { return true;} else { return false; } }, function(err) { console.log(err); return false; }) } /** UPDATE METHOD **/ var ch = checkIfPKExists("p_k"+req.body.id); if(!ch) { res.send("pk does not […]

Mongoosevalidation架构没有模型

我想在对应于较大模式的模型上发布该对象之前,针对作为较大模式的成员的模式来validation对象的一些内部元素。 有人已经问过这个问题, 这个答案在第二个选项上有类似的地方: var p = new People({ name: 'you', age: 3 }); p.validate(function(err) { if (err) console.log(err); else console.log('pass validate'); }); 问题是,在进行validation之前,必须使用var People = mongoose.model('People', peopleSchema);创build一个新模型var People = mongoose.model('People', peopleSchema); 。 这是否意味着在这种情况下,新的集合'People'将被存储在MongoDB数据库中? 是叫mongoose.model('People', peopleSchema); 以任何方式改变数据库? 如果是这样,有没有办法做到这一点,而不是创build一个新的集合,在这种情况下, 'People' ? 我想要validation的模式将作为另一个文档的一个组件存储在数据库中,我不想用未使用的集合来污染数据库。

EventEmitter唤醒侦听器如何计数?

有关警告的问题Warning: Possible EventEmitter memory leak detected. 11 wakeup listeners added. Use emitter.setMaxListeners() to increase limit Warning: Possible EventEmitter memory leak detected. 11 wakeup listeners added. Use emitter.setMaxListeners() to increase limit Warning: Possible EventEmitter memory leak detected. 11 wakeup listeners added. Use emitter.setMaxListeners() to increase limit已经被问了很多。 但是,我没有得到唤醒听众如何计数? Node文档说: eventEmitter.on()方法用于注册侦听器,而eventEmitter.emit()方法用于触发事件。 https://nodejs.org/docs/latest/api/events.html#events_emitter_setmaxlisteners_n 那么,这段代码总结了6个eventEmitter.on(),那么怎么会产生这个警告呢? var express = require('express'); var […]

在Nodejs中实现unit testing – 意外的失败

无法执行unit testing 问题是testing没有通过,尽pipe当我执行testing时请求和响应数据被添加到数据库。 请提出一些方法来通过这个testing。 预期的状态码200没有在这个testing中发生。 集成testing正在对我的应用程序工作,但我无法debugging此unit testing错误。 //testfile var db = require('../mongodb'), mongoose = require('mongoose'), posts = require('../api/addTodo'), should = require('should'), testUtils = require('./utils'); describe("Add Api", function () { var dummyPost, id; before(function (done) { mongoose.connect('mongodb://localhost:27017/todosdb_test', function() { console.log('Connected To:'+'mongodb://localhost:27017/todosdb_test'); done(); }); dummyPost = new db.Todos({ 'admin': 'Dumm_admin', 'text': 'Dummy', 'completed': true }); dummyPost.save(function (err, […]