N
N
Nwton2016-05-15 01:29:11
Node.js
Nwton, 2016-05-15 01:29:11

Collection name must be a String?

There is a code:

var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost:27017/test').connection;

var testdbSchema = new mongoose.Schema({
  param: Number
});

var testdb = mongoose.model('testdb', testdbSchema, function(err){
  if(err)console.log(err);
  console.log('ok');
});

There is a connection to the database, I checked it for err. But when creating a model, it gives the error "collection name must be a String" to the console. What's wrong? Couldn't google anything.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2016-05-15
@Nwton

From your code, I can't figure out what you wanted to do with:
If you would like to save the model to the database, then judging by the documentation , you can do this in 2 ways (adapted to your variables):

var testModel = mongoose.model('TestModel', testdbSchema);

// способ 1
var testRecord = new TestModel({ param: 100500 });
testRecord.save(function (err) {
  if (err) return handleError(err);
  // saved!
})

// или способ 2
TestModel.create({ param: 100500 }, function (err, small) {
  if (err) return handleError(err);
  // saved!
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question