- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从数据库中获取数据,但卡在某个点上。我收到此错误,但我不知道出了什么问题。我试图找到解决方案但仍然没有得到适当的答案。我正在尝试从本教程中实现它。
这是我的代码:
//import express package
var express = require("express");
//import mongodb package
var mongodb = require("mongodb");
//MongoDB connection URL - mongodb://host:port/dbName
var dbHost = "mongodb://localhost:27017/fusion_demo";
//DB Object
var dbObject;
//get instance of MongoClient to establish connection
var MongoClient = mongodb.MongoClient;
//Connecting to the Mongodb instance.
//Make sure your mongodb daemon mongod is running on port 27017 on localhost
MongoClient.connect(dbHost, function(err, db){
if ( err ) throw err;
dbObject = db;
console.log('connect to server');
// console.log(db);
});
function getData(responseObj){
//use the find() API and pass an empty query object to retrieve all records
dbObject.collection("fuel_price").find({}).toArray(function(err, docs){
if ( err ) throw err;
var monthArray = [];
var petrolPrices = [];
var dieselPrices = [];
for ( index in docs){
var doc = docs[index];
//category array
var month = doc['month'];
//series 1 values array
var petrol = doc['petrol'];
//series 2 values array
var diesel = doc['diesel'];
monthArray.push({"label": month});
petrolPrices.push({"value" : petrol});
dieselPrices.push({"value" : diesel});
}
var dataset = [
{
"seriesname" : "Petrol Price",
"data" : petrolPrices
},
{
"seriesname" : "Diesel Price",
"data": dieselPrices
}
];
var response = {
"dataset" : dataset,
"categories" : monthArray
};
responseObj.json(response);
});
}
//create express app
var app = express();
//NPM Module to integrate Handlerbars UI template engine with Express
var exphbs = require('express-handlebars');
//Declaring Express to use Handlerbars template engine with main.handlebars as
//the default layout
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
//Defining middleware to serve static files
app.use('/public', express.static('public'));
app.get("/fuelPrices", function(req, res){
getData(res);
});
app.get("/", function(req, res){
res.render("chart");
});
app.listen("3300", function(){
console.log('Server up: http://localhost:3300');
});
最佳答案
dbObject 为 null,因此没有收集方法。可能 Mongo DB 未运行或正在不同的端口中运行。检查 MongoDB 是否正确启动并运行,并且您可以连接到它。
关于javascript - TypeError : dbObject.集合不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52231918/
抱歉,这是Grails和Mongo的新手问题,因为我很困惑。 我有一个带有嵌套结构的POGO(非Domain类),需要将其存储在MongoDB中。我有下面的示例代码,但这只会生成顶级对象的属性图,并且
我正在尝试从数据库中获取数据,但卡在某个点上。我收到此错误,但我不知道出了什么问题。我试图找到解决方案但仍然没有得到适当的答案。我正在尝试从本教程中实现它。 https://www.fusioncha
我正在尝试创建一个 DBObject 数组,所有元素都具有相同的键和不同的值。这个实现有什么问题? DBObject[] Out = new BasicDBObject[2]; out[0].pu
我正在尝试查询 mongodb,如官方文档页面上的示例所示,但它在 Netbeans 上给出以下错误 bson cannot be converted to DBObject 这是代码 MongoCl
在 mySQL 中,describe 语句可用于检索给定表的架构,不幸的是我找不到 MongoDB java 驱动程序的类似功能:( 假设我有流动的 BSON 文档: { _id: {$oid:49}
我正在编写一个模型工厂,为此我使用 JSON 加载一个 MongoDB DBObject,如下所示: import com.mongodb.util.JSON; DBObject dbObject =
这个问题已经有答案了: MongoDB extracting values from BasicDBObject (Java) (2 个回答) 已关闭 6 年前。 我想将 JSON 数组放入 java
我需要将 MongoDB 的结果放入 Map 中。我的代码是 DBCollection collection = db.getCollection("template"); DBCursor curs
我对 MongoDB 非常陌生,我将它与 Java 驱动程序一起使用。我有这个文档结构: { "_id" : ObjectId("4f7d2ba6fd5a306d82687d48"), "room"
我正在尝试使用 Morphia 库从 MongoDB 检索 Java 对象,如 here 中所述。 。这就需要指定实体类;但是,我将迭代集合列表,并希望能够任意将 DBObject 转换为它们各自的
我有一个来自 mongo fetch 的 mongo db 对象列表 例如 DBObject 包含列名称和顺序: student name,1 student id,2 student address
我有一个 JSON 对象 obj,我想将其存储到 MongoDB 中: JSONObject obj = new JSONObject(); obj.put("title", obj1.title);
我想用 Jersey 发送 Json。我使用 mongoDb。 我的函数返回我的对象: public static List getAll(){ List toReturn = new A
我想知道如何将 DBObject 解析为 JSON,我正在使用 Jersey 框架使用 Java 创建一个 Restful 服务,我想从 MongoDB 加载数据并将其转换为 JSON 格式,最后将其
我有一个使用“test/plain”的 REST 服务,基本上我收到的字符串是一个 JSON 字符串,我有以下代码将 JSON 字符串解析为 DBObject,以便我可以将其保存到 MongoDB。
我有一个 super 类DBAdapter ,它有方法fetchAll()它从数据库表中选择所有记录到 List 。我用 CategoryDBAdapter 继承了这个类,这个类知道类 Categor
这个问题与Java API/驱动程序有关,但也可能与其他语言中的Mongo接口(interface)有关。我是 Mongo 的新手,做了一些假设,所以如果我错了,请纠正我。我找到了很多文档,并且 re
首先我想说的是,我知道有像 Morphia 和 Spring Data for MongoDB 这样的 ORM。我并不是想重新发明威尔——只是为了学习。因此,我的 AbstractRepository
我无法在 spring data mongodb 中查询within的GeoSpatial Query,无论我编写自定义查询还是spring data接口(interface)查询,我都会收到此错误,
我正在制作一款游戏,其中不同的多人服务器中有套件。玩家可以购买套件,并将其保存到 Mongo 数据库中。我希望数据库的格式如下: { "server": "the_server_name",
我是一名优秀的程序员,十分优秀!