【问题标题】:How can I access and insert dates into my database? Node.JS & MongoDB如何访问并将日期插入我的数据库? Node.JS 和 MongoDB
【发布时间】:2016-09-03 10:17:02
【问题描述】:

所以,我正在使用 Node.JS 和 MongoDB 编写日历应用程序。但我无法将数据库中的信息添加到当前日历。

这是我在尝试加载 LocalHost:3000/init 时收到的错误

TypeError:无法读取未定义的属性“插入” 在 /home/patrick/Desktop/Javascript/CalandarApp/server.js:30:13 在 Layer.handle [as handle_request] (/home/patrick/Desktop/Javascript/CalandarApp/node_modules/express/lib/router/layer.js:95:5) 在下一个(/home/patrick/Desktop/Javascript/CalandarApp/node_modules/express/lib/router/route.js:131:13) 在 Route.dispatch (/home/patrick/Desktop/Javascript/CalandarApp/node_modules/express/lib/router/route.js:112:3) 在 Layer.handle [as handle_request] (/home/patrick/Desktop/Javascript/CalandarApp/node_modules/express/lib/router/layer.js:95:5) 在/home/patrick/Desktop/Javascript/CalandarApp/node_modules/express/lib/router/index.js:277:22 在 Function.process_params (/home/patrick/Desktop/Javascript/CalandarApp/node_modules/express/lib/router/index.js:330:12) 在下一个(/home/patrick/Desktop/Javascript/CalandarApp/node_modules/express/lib/router/index.js:271:10) 在 jsonParser (/home/patrick/Desktop/Javascript/CalandarApp/node_modules/body-parser/lib/types/json.js:100:40) 在 Layer.handle [as handle_request] (/home/patrick/Desktop/Javascript/CalandarApp/node_modules/express/lib/router/layer.js:95:5)

这里是 Javascript:

var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');

//Connect to MongoDB
var mongoskin = require('mongoskin');
var db = require('mongodb');
mongoskin.db("mongodb://localhost:3000/testDB", {w: 0});

//Creates Express Application
var app = express();
app.use(express.static('public'));


//is necessary for parsing POST request
app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());

console.log('Express server started on port 3000');

app.get('/init', function(req, res){
    db.event.insert({ 
        text:"Test Event A", 
        start_date: new Date(2016,5,1),
        end_date:   new Date(2016,5,5)
    });
    db.event.insert({ 
        text:"Test Event B", 
        start_date: new Date(2016,5,3),
        end_date:   new Date(2016,5,8),
        color: "#DD8616"
    });

    res.send("Test Events were added to the Database")
});


app.get('/data', function(req, res){
    db.event.find().toArray(function(err, data){
        //set id property for all records
        for (var i = 0; i < data.length; i++)
            data[i].id = data[i]._id;

        //output response
        res.send(data);
    });
});

app.listen(3000); 

【问题讨论】:

  • 我认为您没有正确设置集合。不应该是db.collection('event').insert()。此外,您似乎正在使用 .get.post 路线。如果您要向数据库中插入信息,则需要使用 .post 而不是 .get

标签: javascript node.js mongodb mongoskin


【解决方案1】:

这里是插入代码:

var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var db = new Db(dbName, new Server(host, port), {safe: true});
db.open(function (err, db) {
    if (err) {
      return callback(err);
    }
    db.collection('collectionName', function (err, collection) {
      if (err) {
        mongodb.close();
        return callback(err);
      }
      collection.insert(post, {
        safe: true
      }, function (err) {
        mongodb.close();
        if (err) {
          return callback(err);
        }
        callback(null);
      });
    });
  });

这里是访问代码:

 db.open(function(err, db) {
    if(err) {
      return callback(err);
    }
    db.collection('collectionName', function(err, collection) {
      if(err) {
        mongodb.close();
        return callback(err);
      }
      collection.findOne({
        query Condition
      }, function(err, result) {
        db.close();
        if(err) {
          return callback(err);
        }
        callback(null, result);
      })
    });
  });

【讨论】:

    【解决方案2】:

    你得到Cannot read property 'insert' of undefined 因为db.event 是未定义的。您需要使用db.bind('COLLECTION_NAME') 将mongoskin 事件绑定到集合名称。

    另外,db 应该使用 mongoskin 创建,您不需要 require('mongodb')

    您的代码可以这样开始:

    // load mongoskin module
    var mongo = require('mongoskin');
    
    // connect to local mongo database
    var db = mongo.db('127.0.0.1:27017/test');
    
    // bind db.event to db.collection('event') so you can use db.event.insert
    db.bind('event');
    
    // rest of tour code here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多