我對編程很陌生,正在按照教程學習。我被卡住了,無法使用代碼發布新條目,也無法找到我在這里遺漏的內容。任何幫助將不勝感激。當我嘗試使用郵遞員發帖時,我收到驗證錯誤,當我嘗試獲取值時,我收到 []。編輯:錯誤消息:“msg”:“錯誤:ValidationError:first_name:first_name需要路徑。,last_name:last_name需要路徑。,電子郵件:email需要路徑?!眪// importing modulesvar express = require('express');var mongoose = require('mongoose');var bodyparser = require('body-parser');var cors = require('cors');var path = require('path');var app = express();const route = require('./routes/route');//connect to mongoDBmongoose.connect('mongodb://localhost:27017/contactlist');//on connectionmongoose.connection.on('connected', () => { console.log('Connected to database mongoDB @ 27017');});//on errormongoose.connection.on('error', (err) => { if (err) { console.log('Error in DB connection' + err); }});//port noconst port = 3000;//adding middlewareapp.use(cors());//body - parserapp.use(bodyparser.json());//static filesapp.use(express.static(path.join(__dirname, 'public')));//routesapp.use('/api', route);//testing serverapp.get('/', (req, res) => { res.send('cutard');});app.listen(port, () => { console.log('Server started at port:' + port);});const express = require('express');const router = express.Router();const Contact = require('../models/contacts');//retriving contactrouter.get('/contacts', (req, res, next) => { Contact.find(function (err, contacts) { res.json(contacts); })});//add contactrouter.post('/contacts', (req, res, next) => { console.log(req.body) let newContact = new Contact({ first_name: req.body.first_name, last_name: req.body.last_name, email: req.body.email }); newContact.save((err, Contact)=>{ if (err) { res.json({ msg: ' Error: '+err}); } else { res.json({ msg: 'Contact added successfully' });; } });});
Nodejs無法發帖
森林海
2022-07-08 15:55:27