如果这不符合社区标准,我深表歉意,但请注意,我已尽力而为。
我在从 MongoDB 实例获取内容的 API 中返回 JSON 响应时遇到问题
基本上我有以下文件来处理 nodejs 服务器 (server.js):
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Person = require('./models/people');
mongoose.connect('mongodb://localhost/testdb');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var router = express.Router();
router.use(function (req,res,next) {
console.log('something going on');
next();
})
router.get('/', function (req,res) {
res.status(500).json({
error: 'unauthorized'
});
});
router.route('/getPeople').get(function (req,res){
Person.find(function (err,people) {
console.log('err: ' , err);
console.log('people: ' , people);
if (err)
res.send(err)
res.json(people);
})
})
app.use('/api',router);
app.listen(port);
console.log('server started on port : ' , port);
模型如下(people.js):
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var PeopleSchema = new Schema({
name: String,
surname: String
});
module.exports = mongoose.model('Person',PeopleSchema)
我的数据库正确填充了一个值,如果我使用 mongo shell (db.People.find()) 访问它,我会得到正确的结果。相反,使用代码,它不起作用。
最佳答案
您需要在 find() 中指定条件或搜索条件对象 方法,它在您的代码中丢失。您只包含了回调函数。尝试重写路由方法并更改模型名称以反射(reflect)数据库名称:
people.js('./models/people'):
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var PeopleSchema = new Schema({
name: String,
surname: String
});
module.exports = mongoose.model('People', PeopleSchema);
或者如果您希望将模型名称保留为 Person,您可以将 collectionName 作为 model() 函数参数中的第三个参数传递:
module.exports = mongoose.model('Person', PeopleSchema, 'People');
或作为模式定义中的属性:
var PeopleSchema = new Schema({
name: String,
surname: String
}, { collection: 'People' });
module.exports = mongoose.model('Person', PeopleSchema);
服务器(server.js):
/* if using the renamed People model */
router.route('/getPeople').get(function (req,res){
People.find({}, function (err, people) {
console.log('err: ' , err);
console.log('people: ' , people);
if (err)
res.send(err)
res.json(people);
});
});
或通过执行查询:
/* if using the renamed People model */
router.route('/getPeople').get(function (req,res){
People.find().exec(function (err, people) {
console.log('err: ' , err);
console.log('people: ' , people);
if (err)
res.send(err)
res.json(people);
});
});
关于javascript - 获取从 nodeJS + Express 返回的空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35080689/
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
所以我开始关注ruby,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出