I am running the following code to insert fruit names into the database with Mongoose. It does insert the object but then it throws a TypeError: Unknown encoding: 1 and exits the script. I tried updating Mongoose and Mongo but it did not solve the problem.
var mongoose = require('mongoose');
var assert = require('assert');
var Fruits = require('./models/fruits-1');
var url = 'mongodb://localhost:27017/confusion';
mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function(){
Fruits.create(
{
name: 'Apple',
description: "It's delicious."
}, function(err, fruit){
if(err){
console.log(err);
}
else{
console.log(fruit);
}
});
});
I have a bad working solution, remove console.log(fruit).
I had the same problem now on Fedora 25, with the same kind of code. And it worked normally sooner today on Ubuntu 14.04.
Both of them are using Nvm with v6.
The real solution is to update your bson to 1.0.3 - see this issue (thanks Blaze Sahlzen).