我在端口3000和4000上创建了2个Web服务器。 其中一个网络服务器创建了一个数据库,并有3个集合..
显示dbs 本地0.000GB sensor_db 0.000GB 使用sensor_db 切换到db sensor_db 显示集合 传感器 templategroups 模板
第二台服务器可以访问此数据库吗?如果是,我无法访问集合..是否有任何语法?
var express = require('express');
var app= express();
var path = require('path');
var bodyParser= require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/sensor_db');
var Schema = mongoose.Schema;
var sensorSchema = new Schema({
value:{ type:Number, default:0},
format:{type:String, default:"default"},
id:{type:Number,required:true,unique:true},
description:{type:String},
type:{type:String},
groupId:{type:Number},
users:{type:Array,default:[]},
admin:{type:String,default:'Undefined'},
status:{type:String,default:'Undefined'},
owner:{type:String,default:'Undefined'},
templateId:{type:Number}
});
var Sensor = mongoose.model('Sensor',sensorSchema);
app.get('/sensorlist',function(req,res) {
console.log("I recieved a GET /sensorlist request");
Sensor.find(function(err,data){
if (err) return console.error(err);
console.log(data);
res.json(data)
});
});
app.post('/check/health',function(req,res){
socket.emit('data', 'I need your health status', function ack(data) {
console.log('data emit was acknowledged by Monitoring Server:', data);
return res.json(data);
});
});
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var express = require('express');
io.on('connection', function(socket){
console.log('connection received from Provisioning');
// To get messages from Provisioning server
socket.on('data', function(data, ack) {
console.log('Message from provision is : ' + ': ' + data);
ack('here is your data - 1111');
console.log("Trying to access the Sensor_DB Database");
Sensor.find(function(err,data){
if(err) return console.error(err);
console.log(data);
//res.json(data);
});
});
});
server.listen(4000, function(){
console.log('socket.io server listening on *:4000');
});
我收到错误 - 传感器未定义
非常感谢 杰施
我尝试在连接到数据库后调度集合,但收到此错误消息:无法读取属性&hasCollectionsCommand'为null
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/sensor_db') ;
console.log("successfully connected to the database");
//mongoose.connection.db
mongoose.connection.db.listCollections().toArray(function(err, names) {
if (err) {
console.log(err);
}
else {
names.forEach(function(e,i,a) {
mongoose.connection.db.dropCollection(e.name);
console.log("--->>", e.name);
});
}
});
答案 0 :(得分:0)
Two Different servers cannot share the same instance of the sensor object.
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/sensor_db');
var Schema = mongoose.Schema;
var sensorSchema = new Schema({
value:{ type:Number, default:0},
format:{type:String, default:"default"},
id:{type:Number,required:true,unique:true},
description:{type:String},
type:{type:String},
groupId:{type:Number},
users:{type:Array,default:[]},
admin:{type:String,default:'Undefined'},
status:{type:String,default:'Undefined'},
owner:{type:String,default:'Undefined'},
templateId:{type:Number}
});
var Sensor = mongoose.model('Sensor',sensorSchema);
This code declaring the schema for one server not for the second . so you have to declare the instance in the second server also.
Issue not with the mongo data base issue is the sensor instance that is not have any declaration in second server.