gpt4 book ai didi

node.js - Mongoose .save() 无法在没有回调的情况下工作

转载 作者:可可西里 更新时间:2023-11-01 09:14:12 36 4
gpt4 key购买 nike

当我执行 API 时 http://localhost:8080/api/bears发布

控件被映射到 post 路由并进入 .save() 之后它不返回 'err' 回调。

我尝试了两个数据库的全局 Mongoose 连接,但没有帮助。

server.js

// BASE SETUP
// =============================================================================

// call the packages we need
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var morgan = require('morgan');

// configure app
app.use(morgan('dev')); // log requests to the console

// configure body parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080; // set our port

var mongoose = require('mongoose');
//mongoose.connect('mongodb://node:node@novus.modulusmongo.net:27017/Iganiq8o'); // connect to our database
mongoose.connect('mongodb://zohaibusr:asd123@ds039115.mongolab.com:39115/zohaibdb'); // connect to our database

var Bear = require('./app/models/bear');

// ROUTES FOR OUR API
// =============================================================================

// create our router
var router = express.Router();

// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next();
});

// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});

// on routes that end in /bears
// ----------------------------------------------------
router.route('/bears')

// create a bear (accessed at POST http://localhost:8080/api/bears)
.post(function(req, res) {

var bear = new Bear(); // create a new instance of the Bear model
bear.name = req.body.name; // set the bears name (comes from the request)

bear.save(function(err) {
if (err)
res.send(err);

res.json({ message: 'Bear created!' });
});


})

// get all the bears (accessed at GET http://localhost:8080/api/bears)
.get(function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);

res.json(bears);
});
});

// on routes that end in /bears/:bear_id
// ----------------------------------------------------
router.route('/bears/:bear_id')

// get the bear with that id
.get(function(req, res) {
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
res.json(bear);
});
})

// update the bear with this id
.put(function(req, res) {
Bear.findById(req.params.bear_id, function(err, bear) {

if (err)
res.send(err);

bear.name = req.body.name;
bear.save(function(err) {
if (err)
res.send(err);

res.json({ message: 'Bear updated!' });
});

});
})

// delete the bear with this id
.delete(function(req, res) {
Bear.remove({
_id: req.params.bear_id
}, function(err, bear) {
if (err)
res.send(err);

res.json({ message: 'Successfully deleted' });
});
});


// REGISTER OUR ROUTES -------------------------------
app.use('/api', router);

// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);

熊图式

var mongoose     = require('mongoose');
var Schema = mongoose.Schema;

var BearSchema = new Schema({
name: String
});

module.exports = mongoose.model('Bear', BearSchema);

编辑:添加截图

enter image description here

最佳答案

我发现了我的错误。

建立的连接错误。

我使用全局 mongo 数据库从 mongolab.com 快速启动

但是在本地设置之后。成功了

mongoose.connect('mongodb://localhost:27017/zohaibdb');

enter image description here

关于node.js - Mongoose .save() 无法在没有回调的情况下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34590197/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com