尝试将数据放入MongoDB时无法读取未定义的属性“插入”

odinas1212

我收到此错误:尝试将数据插入数据库时​​,“无法读取未定义的属性'插入'”。错误显示在:

db.coordinates。插入({“ x”:“ data.x”,“ y”:“ data.y”})

数据库名称-“ node5”集合名称-“坐标”

// Including libraries
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var static = require('node-static'); // for serving files

//db connection

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/node5');



var url = 'mongodb://localhost:27017/node5';
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server.");
  db.close();
});

// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('./');

// This is the port for our web server.
// you will need to go to http://localhost:8080 to see it
app.listen(8080);

// If the URL of the socket server is opened in a browser
function handler(request, response) {
    request.addListener('end', function () {
        fileServer.serve(request, response);
    }).resume();
}

// Delete this row if you want to see debug messages
io.set('log level', 1);


// Listen for incoming connections from clients
io.sockets.on('connection', function (socket) {
    // Listen for mouse move events
    socket.on('post', function (data) {
        console.log('posted');
        console.log(data);
        socket.broadcast.emit('posted', data); // Broadcasts event to everyone except originating client
        db.coordinates.insert({ "x" : "data.x", "y" : "data.y"})
    });
});

编写答案时,请注意,我是node.js的新手,如果您以复杂的方式讲出答案,我可能会不明白:)

乌扎尔·萨吉德(Uzair Sajid)

如果您要monk用于项目,则可以删除该mongodb模块,因为它的功能已由和尚包装。根据Monk的文档,您应该执行以下操作:

const monk = require('monk');
const db = monk('localhost:27017/node5')

const coordinates = db.get('coordinates');

既然您已经有了对坐标集合的引用,则可以在以后的代码中使用它:

coordinates.insert({ x: data.x, y: data.y });

我希望这很容易理解。如果仍然令人困惑,请在下面发表评论,我将进一步详细说明:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法读取未定义的属性“类别”-尝试使用状态中的数据时出错

无法读取数据加载时未定义的属性

显示将数据插入mongoDB时未定义db

无法读取未定义的属性“数据”

将行放入setTimeout后,无法读取未定义的属性“ substring”

尝试检查属性是否未定义时如何防止“无法读取未定义的属性示例”

未捕获(承诺)TypeError:尝试在ReactJs中捕获API数据时,无法读取未定义的属性

Uncaught TypeError:尝试使用PHP填充响应数据表时,无法读取未定义的属性“ length”?

尝试尝试.filter()时,TypeError:“无法读取未定义的属性'toLowerCase'”

TypeError:尝试读取通过props传递的数组时,无法读取未定义的属性“ map”

使用节点JS将数据添加到MongoDB时,我收到以下错误消息:“ TypeError:无法读取未定义的属性'username'”

尝试在单击时播放音频文件时,无法读取未定义的属性“ keyCode”

从mongoDB读取环境变量时,“无法读取未定义的属性'0'”

无法读取未定义尝试创建嵌入的属性“MessageEmbed”

尝试映射数组时出错。类型错误:无法读取未定义的属性“地图”

尝试单击窗口加载时无法读取未定义触发器的属性

尝试渲染布局视图时无法读取未定义的属性“ render”

尝试创建Google图表时遇到错误Uncaught TypeError:无法读取未定义的属性'arrayToDataTable'

Uncaught TypeError:尝试使用React MuiAlert时无法读取未定义的属性“ main”

尝试复制时粘贴输入字段的值:无法读取未定义的属性“ nodeType”

尝试发出文档时无法读取未定义的属性“发出”

discord.js尝试赋予角色时无法读取未定义的属性添加

尝试使用Apollo服务器缝合架构时,无法读取未定义的属性'args'

错误TypeError:尝试使用formArray时无法读取未定义的属性“ map”

当我尝试制作 ID 时;“无法读取未定义的属性‘长度’。”

尝试搜索列表时出现“类型错误:无法读取未定义的属性‘过滤器’”

TypeError:尝试使用LocalStorage而不是useState时,无法读取未定义的属性“ map”

尝试访问存储变量时,React Redux“无法读取未定义的属性'状态'”错误

TypeError:尝试映射多个数组时无法读取未定义的属性“ map”