gpt4 book ai didi

javascript - 错误 : collection name must be a String in node js

转载 作者:行者123 更新时间:2023-11-30 07:36:04 24 4
gpt4 key购买 nike

我正在尝试在服务器上运行以下代码

var express = require("express"),
app = express(),
bodyParser = require('body-parser'),
errorHandler = require('errorhandler'),
methodOverride = require('method-override'),
hostname = process.env.HOSTNAME || 'localhost',
port = parseInt(process.env.PORT, 10) || 4004,
publicDir = process.argv[2] || __dirname + '/public';
var exec = require('child_process').exec;
var fs = require('fs');
var mongodb = require('mongodb'),
serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
dbName = new mongodb.Db('prisync', serverdb, {});
var url = "urlinfo";


//Show homepage
app.get("/", function (req, res) {
//res.redirect("/index.html");
console.log("shubham ");
dbName.open(function (error, client){
var collection = new mongodb.Collection(client , url); //line 24 ====
collection.find().limit(20).toArray(function (err, dataObjArr){
var data = '';
var dataArr = [];
var i = dataObjArr.length;
//check for error
if(err){return res.end('error!'+err);}
//Data
if(dataObjArr){
while(i--){
dataArr[i] = dataObjArr[i]._id;
}
data = dataArr.join(' ');
res.render('/index.html',{ returnedData : data });
}else{
res.end();
}

});


});
});



app.get("/search", function (req, res){
console.log("shubham batra");
var pro_name = req.query.name;
var pro_code = req.query.code;
var pro_category = req.query.category;
var pro_brand = req.query.brand;

pro_name = pro_name+"";
pro_code = pro_code+"";
pro_brand = pro_brand+"";
pro_category = pro_category+"";



MongoClient.connect('mongodb://127.0.0.1:27017/prisync', function(err, db) {
if (err) throw err;
console.log("Connected to Database");


var documen = {name:pro_name, code:pro_code , category:pro_category, brand:pro_brand };
//documen = JSON.stringify(documen);
//insert record
db.collection('urlinfo').insert(documen, function(err, records) {
if (err) throw err;

});

res.redirect("/index.html");

});
});
//Search page
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(publicDir));
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));

console.log("Server showing %s listening at http://%s:%s", publicDir, hostname, port);
app.listen(port);

但在尝试加载 localhost:4004

时出现以下错误
Server showing /home/shubham/Music/pricesync/server/public listening at http://localhost:4004
shubham

/home/shubham/node_modules/mongodb/lib/server.js:274
process.nextTick(function() { throw err; })
^
Error: collection name must be a String
at Error (<anonymous>)
at checkCollectionName (/home/shubham/node_modules/mongodb/lib/utils.js:69:11)
at new Collection (/home/shubham/node_modules/mongodb/lib/collection.js:57:3)
at /home/shubham/Music/pricesync/server/server.js:24:24
at /home/shubham/node_modules/mongodb/lib/db.js:221:5
at connectHandler (/home/shubham/node_modules/mongodb/lib/server.js:272:7)
at g (events.js:180:16)
at emit (events.js:95:17)
at /home/shubham/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:399:23
at /home/shubham/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:756:13

最佳答案

我有同样的错误,对我来说这是由于我的时间戳放置不正确造成的。


错误代码:

    }),{timestamps:true});


更正代码:

var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;

var User = mongoose.model('User', new mongoose.Schema({
email:{type:String, required:true},
password:{type:String, required:true},

listings:[{type:ObjectId,ref:"Listings"}]

},{timestamps:true}));

关于javascript - 错误 : collection name must be a String in node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32375478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com