Tag: 表示

保存function在expressjs mongoose中不起作用

我正在尝试将表单数据保存到mongodb数据库中。 当我尝试使用.Find方法时,它从用户集合中返回数据。 但是当我尝试保存数据时,它显示一条错误消息。 .Save方法也应该使用相同的Schema,但它不是。这里是我的代码: var express= require('express'); var mongoose = require('mongoose'); var path = require('path'); var bodyParser = require('body-parser'); var cookieParser = require('cookie-parser'); var app= express(); /* set view folder and view engine */ app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false})); app.use(cookieParser()); /* database connection */ mongoose.connect('mongodb://localhost:27017/expresstest', function (err) { if (err) { […]

同构提取不适用于外部请求?

编辑:我实际上希望了解为什么响应不包含我请求的数据,是否由于某些库丢失或我的fetchUrlvariables的格式 您好我试图使用同构的fetch方法,使ajax请求,并努力从外部API获取数据。 不应该这个工作,如果不是我错过了什么? require('es6-promise').polyfill(); require('isomorphic-fetch'); router.get('/weather', function(req, res){ var fetchUrl = 'http://api.wunderground.com/api/xyz-token/conditions/q/CA/San_Francisco.json'; fetch(fetchUrl, { method: "GET" }) .then(function(response){ if (response.status >= 400) { throw new Error("Bad request response from server"); } console.log(response); res.send(response); }); }); 我的回复如下所示: {“url”:“ http://api.wunderground.com/api/xyz-token/conditions/q/CA/San_Francisco.json ”,“status”:200,“statusText”:“确定”,“标题” :{“_ headers”:{“server”:[“Apache / 2.2.15(CentOS)”],“access-control-allow-origin”:[“*”],“access-control-allow-credentials” [“true”],“x-creationtime”:[“0.140”],“content-encoding”:[“gzip”],“last-modified”:[“Tue,03 May 2016 10:47:33 GMT” ]“content-type”:[“application / json; charset = UTF-8”],“content-length”:[“1043”],“vary”:[“Accept-Encoding”],“expires”: [“2016年5月3日星期二10:48:58 […]

当您有子资源时,为API – URL处理程序快速路由

我有两个资源,员工和员工组。 我试图实现一个漂亮的URL结构,如: GET /employees列出员工。 GET /employees/123获取员工123。 GET /employees/groups列出员工组。 GET /employees/groups/123获取员工组123。 使用ExpressJS我有: router.get('/employees', (req, res, next) => { next(); }); router.get('/employees/:id', (req, res, next) => { next(); }); router.get('/employees/groups', (req, res, next) => { next(); }); router.get('/employees/groups/:id', (req, res, next) => { next(); }); router.all('*', (req, res) => { res.send('…'); }); 这是行不通的,因为Express不能区分/employees/:id和/employees/groups 。 它认为groups是一个id因为/employees/:id是第一位的。 我确实有URL: GET […]

ExpressJS / NodeJS / Promises:从承诺链早期返回

当我在服务器上发布一个请求来创build一个新的游戏时,我会执行一些查询。 首先,我search用户是否已经在游戏中,如果是,则返回游戏。 否则,我search一个开放的游戏,在这个游戏中有人正在等待对手,如果是这样的话,就返回那个游戏。 最后,如果没有发现上述状态的游戏,我创build一个新游戏并返回。 所以我的代码看起来像这样: .post( function(req, res, next){ …findUsersExistingGame… .then(function(game){ if(game){ return res.send(game); } else{ return …findUserWaitingForOpponentsGame… } } .then(function(game){ if(game){ return res.send(game); } else{ return …createNewGame… } }) .then(function(game){ return res.send(game); }) .catch(function(err){ return next(err); }); 我最终将每个函数重构为辅助函数,以提高可读性,但是我需要首先确定链接。 我的问题是,如果我在承诺链中早期发现一个游戏(即,有一个用户的现有游戏或另一个正在等待对手的用户),则返回res.send(游戏)。 然而,第三个.then会抛出一个错误,因为我以前的.then()语句返回undefined。 如果我想做一个res.send(游戏),我如何早日退出承诺链? 选项1:我已经看到了一些build议来抛出一个错误,并明确地捕捉错误,但是这种感觉从根本上是错误的,使用错误来控制stream量。 scheme2:我可以做这样的事情,而不是链接承诺,但是类似于“promise / callback hell”: .post( function(req, res, next){ …findUsersExistingGame… .then(function(game){ if(game){ return […]

将数据推送到MongoDB,而不需要编辑MEAN中的整个条目

所以我有一个单一的页面应用程序通过MEAN堆栈运行。 我有一个Klausur的计划/模型(Klausur是德语考试,我想pipe理考试)。 var KlausurSchema = new Schema( { name: String, semester: String, krankmeldungen: [Number], aufgaben: [{ name: String, punkte: Number }], studenten: [{ matrnr: Number, vorname: String, nachname: String, bewertung: [{ punkte: Number }], pversuch: String, pvermerk: String, freiverm: String, labnr: Number, porgnr: Number, aenddat: String }] } ); 多个用户可以编辑entrys,否则我只会覆盖条目。 我想要一张由“学生”组成的桌子,是否可以把一个学生推到我的“学生”,而不需要对整个“Klausur”进行PUTTING(编辑),也就是说,我想把信息推送到一个数组而不覆盖整个数据库条目! 提前致谢!

如何用可选的快速查询参数编写Mongoose查询?

我正在Express 4之上build立一个REST API。对于一个path可以说GET /api/users我想查询我的用户集合相应的可选 queryParams。 所以对API的调用可能看起来像这个host:port/api/users?city=Berlin 在这种情况下, name被省略,不影响查询,而city必须匹配。 我设法做到这一点与下面的黑客。 有没有比这更好的方法? Users.statics.customFilter = function(qP){ return this.find({ $and: [ {$or: [{undefined: {$eq: qP.city}}, {'city': qP.city}]}, {$or: [{undefined: {$eq: qP.name}}, {'name': qP.name}]} ] }); mongoose.model('User', Users); 我从这样的mongoose模式调用这个静态函数… const User = mongoose.model('User'); app.get('/api/users', (req, res) => { User.customFilter(req.query) .exec((err, results) => { if (err) return next(err); res.json(results); }); });

获取“TypeError:调用`npm start`时无法读取未定义的属性”文件名“

我正在关注这个相当冗长但非常丰富的post,关于使用以下技术堆栈设置Web应用程序: es 6 节点+expression 车把 反应+反应路由器 webpack + babel 示例代码可以在这里find。 我一直在跟着,一切都很顺利,我理解了最重要的概念。 但是,当我尝试运行它时(使用git clone下载之后)出现错误,如下所示: $ npm install $ npm start 生成的输出包括错误是: > react-ssr-example@0.0.1 start /Users/nburk/Developer/node/templates/react-universal-web-apps-simple > npm-run-all –parallel gulp server > react-ssr-example@0.0.1 server /Users/nburk/Developer/node/templates/react-universal-web-apps-simple > node-dev app/babel-server.js > react-ssr-example@0.0.1 gulp /Users/nburk/Developer/node/templates/react-universal-web-apps-simple > gulp [19:10:39] Using gulpfile ~/Developer/node/templates/react-universal-web-apps-simple/gulpfile.js [19:10:39] Starting 'build:scss'… [19:10:39] Starting 'build:watch:app'… TypeError: Cannot read […]

制作和使用NodeJS的soap客户端同步

当我向服务器发送一个HTTP请求时,使用NodeJs的当前soap客户端来调用soap服务函数。 问题是请求在soap服务调用完成之前返回,因此没有返回数据。 这里的问题是,逻辑是asynchronous的,但我需要使其同步。 请亲切指导我到正确的解决scheme。 function soap_get_items(req,res){ var resp = []; var soap = require('soap'); var url = 'http://example.com/example.wsd?singleWsdl'; var args = {name:"N.Hillary"}; soap.createClient(url, function(err, client) { client.exampleSoapFunction(args, function(err, result) { resp = result; }); }); json_resp = JSON.stringify(resp); res.end(json_resp); }

发送后无法设置标题 – 通过NodeJs发布新用户

我正尝试使用Google Maps API , NodeJs , Express , MongoDB , Mongoose和AngularJS来构build应用程序,而我面临的问题是我无法解决查看其他相关SO Q / A的问题。 基本上,我试图post到由username和[latitude, longitude]标识的我的db用户[latitude, longitude]他们提交了我在我看来的某种forms。 当我尝试从Postman等应用程序直接post用户时,一切正常,我可以在我的数据库中看到新用户。 相反,当我尝试直接发布用户提交时,我的控制台中出现以下错误: /node_modules/mongodb-core/lib/topologies/server.js:766 catch(err) { process.nextTick(function() { throw err}); } ^ Error: Can't set headers after they are sent. 以及我的Google Chrome控制台中的以下日志: angular.js:10695 GET http://localhost:3000/users net::ERR_CONNECTION_REFUSED 这是我的观点 : <form name="addForm" novalidate> <div class="form-group"> <label for="username">Username <span class="badge">All fields required</span> […]

无法导入我的路线模块

当我尝试使用app.use(require(./routes))将我的路由模块导入到主应用中时,我得到app.use() requires middleware functions 。 我应该如何去module.exportsfunction,使其工作? 我的路线: var express = require('express'), auth = require('../middleware/auth.js'), user = require('../models/user.js'), formidable = require('formidable'); module.exports = (function () { app.post('/', function (req, res) { var form = new formidable.IncomingForm(); form.parse(req, function (err, fields, files) { user.create(fields.username, fields.email, fields.password); }); res.render('./game/game.html', {}); }); app.get('/', function (req, res) { res.render('./index.html', {}); […]