Question: Getting TypeError: Unknown encoding: 1 while running the server script with Mongoose

Question

Getting TypeError: Unknown encoding: 1 while running the server script with Mongoose

Answers 2
Added at 2017-01-04 07:01
Tags
Question

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);
            }
    });
});

enter image description here

Answers
nr: #1 dodano: 2017-01-04 18:01

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.

nr: #2 dodano: 2017-01-05 06:01

The real solution is to update your bson to 1.0.3 - see this issue (thanks Blaze Sahlzen).

Source Show
◀ Wstecz