This is my model with the scheme itself bd

import mongoose from 'mongoose'; import bcrypt from 'bcryptjs'; import 'mongodb'; mongoose.connect('mongodb://localhost:27017/users'); mongoose.connection; var UserSchema = mongoose.Schema({ name: { type: String, index: true }, username: { type: String }, password: { type: String }, email: { type: String } }); var User = module.exports = mongoose.model('User', UserSchema); function createUser(newUser, callback) { bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(newUser.password, salt, function(err, hash) { newUser.password = hash; return newUser.save(callback); }); }); } module.exports = createUser; 

And this is when I post a request, I'm trying to save all the data in the database

 var newUser = new User({ name: name, email: email, username: username, password: password }); User.createUser(newUser, function(err, user) { if(err) throw err; console.log(user); }); 

But what gives the console

 { name: 'name', user_name: 'Aleksey Bilous', user_email: 'alekseybilous@gmail.com', user_pass: '1', user_passConf: '1' } /home/leha/Desktop/Projects/test/models/user.js:44 return newUser.save(callback); ^ TypeError: newUser.save is not a function 

the data itself comes all well, but why doesn’t it want to accept the save method, do you have ideas ??

  • Purely according to js, ​​he swears right - User has no implementation of the save method and he does not understand where to get it from. MB here you will find the answer docs.mongodb.com/v3.0/reference/method/db.collection.save . Judging from what was written, save should be applied to the collection, passing the new entry newUser - alexoander as a parameter.

0