это мой средний код.
const express = require('express');
const router = express.Router();
const passport = require('passport');
const jet = require('jsonwebtoken');
const Contact = require('../models/contacts');
// retrieving Data
router.get('/contacts',(req,res,next)=>{
// res.send('Retriving the contact list');
console.log('contacts page');
Contact.find(function(err, contacts){
res.json(contacts);
})
});
// to add the content
router.post('/contact',(req, res, next)=>{
// logic to add contact
let newContact = new Contact({
first_name: req.body.first_name,
last_name: req.body.last_name,
email_id: req.body.email_id,
password: req.body.password
});
Contact.addRegistry((err, contacts)=> {
if(err) {
res.json({msg:'faild to add register'});
}
else{
res.json({msg:'registry added sucessfully'});
}
});
});
// to delete the content
router.delete('/contact/:id',(req, res, next) =>{
// logic to delete contact
Contact.remove({_id:req.params.id}, function(err, result){
if(err){
res.json(err);
}
else {
res.json(result);
}
});
})
module.exports = router;
вышеупомянутый файл - route.js.приведенный ниже код от contact.js
// Database code.
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
// database schaema
var ContactSchema = new mongoose.Schema({
first_name: String,
last_name: String,
id: String,
location: String,
profile_picture_url: String,
email_id: String,
phone: String,
job_title: String,
company: String,
education: String,
password: String,
savedjobslist: {
title: [],
subtitle: []
},
appliedjobslist: {
title: [],
subtitle: []
},
failedjobslist: {
title: [],
subtitle: []
}
});
const Contact = module.exports = mongoose.model('Contact', ContactSchema);
module.exports.getUserById = function(id,callback) {
Contact.findById(id,callback);
}
module.exports.getUserById = function(username,callback) {
const query = {username: username}
Contact.findOne(query,callback);
}
module.exports.addRegistry = function(newContact,callback) {
bcrypt.genSalt(10, (err,salt) => {
bcrypt.hash(newContact,salt, (err,hash) => {
if (err) {
console.log(err);
}
newContact.password = hash;
newContact.save(callback);
});
});
}
Я пытаюсь опубликовать данные почтальона, он выдает ошибку, так как
"произошла ошибка при подключении к http://localhost:3000/api/contact"
и в командной строке отображается ошибка:
Сервер запущен на порту 3000, подключен к базе данных mongos в 27017 Ошибка: недопустимые аргументы: функция, строка в _async(D: \ project-1 \ back-end \ node_modules \ bcryptjs \ dist \ bcrypt.js: 214: 46) в Object.bcrypt.hash (D: \ project-1 \ back-end \ node_modules \ bcryptjs \ dist \bcry pt.js: 220: 13) в bcrypt.genSalt (D: \ project-1 \ back-end \ models \ contacts.js: 49: 16) в Immediate._onImmediate (D: \ project-1 \ back-end\ node_modules \ bcryptjs \ dist \ bcrypt.js: 153: 21) в runCallback (timers.js: 794: 20) в tryOnImmediate (timers.js: 752: 5) в processImmediate [как _immediateCallback] (timers.js: 729:5) D: \ project-1 \ back-end \ models \ contacts.js: 54 newContact.save (обратный вызов); ^
TypeError: newContact.save не является функцией в bcrypt.hash (D:\ project-1 \ back-end \ models \ contacts.js: 54: 23) в runCallback (timers.js: 794: 20) в tryOnImmediate (timers.js: 752: 5) в processImmediate [как _immediateCallback] (timers.js: 729: 5) Сбой приложения [nodemon] - ожидание изменений файла перед запуском ...
newContact.save (callback);^ TypeError: newContact.save не является функцией.я не знаю, почему появляется эта ошибка.