在我的查找功能中。参数“item”始终为 Null。我想知道插入或查找我的代码是否有问题。我该怎么解决??
var express = require('express');
var routes = require('./routes');
var socket = require('socket.io');
var fs = require('fs');
var app = module.exports = express.createServer();
var Server = require('mongodb').Server,
Db = require('mongodb').Db,
Connection = require('mongodb').Connection;
var host = 'localhost';
var port = Connection.DEFAULT_PORT;
var db = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:false});
var id ;
db.open(function(err, db) {
//console.log('opened');
app.listen(3000);
});
db.collection('locations', function(err, collection) {
var object= ({'word':'TEST'},{'word':'HELL'},{'img':'terr'});
collection.insert(object, function(err, result) {
console.log(result);
id = object._id;
collection.find({'word':'TEST'}).toArray(function(err, items){
console.log('item' +items);// <-- The item is always NULL
});
});
});
然后我将我的代码更改为....
但它仍然有关于“无法调用null的方法集合”的错误。
db.open(function(err, db) {
db.collection('locations', function(err, collection) {
var object= ({'word':'TEST'},{'word':'HELL'},{'img':'terr'});
collection.insert(object, function(err, result) {
console.log(result);
id = object._id;
collection.find({'word':'TEST'}).toArray(function(err, items){
if(err)
console.log(err);
else
console.log('item' +items);
});
});
});
app.listen(3000);
});