这是我的代码:
//server.js
const express = require('express'),
cors = require('cors'),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
const Mate = require('./models/mate');
mongoose.Promise = global.Promise;
const app = express();
const router = express.Router();
app.use(cors());
app.use(bodyParser.json())
mongoose.connect('mongodb://localhost:27018/mate', {useNewUrlParser: true});
const connection = mongoose.connection;
connection.once('open', () => {
console.log('Mongodb db connection established succ');
})
router.route('/mate').get((req, res) => {
mongoose.model('Mate').find((err, mate) => {
if (err)
console.log(err);
else
res.json(mate);
});
});
router.route('/mate/:id').get((req, res) => {
mongoose.model('Mate').findById((err, mate) => {
if(err)
console.log(err);
else
res.json(mate);
});
});
router.route('/mate/add').post(async (req, res) => {
try {
const mate = new Mate(req.body);
await mate.save();
res.status(200).json({
'mate:': 'Added succesfully'
});
} catch (err) {
console.log(err);
res.status(400).send( 'failed to create' );
}
});
router.route('mate/update/:id').post((req, res) => {
mongoose.model(Mate).findById(req.params.id, (err, mate) =>{
if(!mate)
return next(new Error('couldnt load doc'))
else {
mate.title = req.body.title;
mate.day = req.body.day;
mate.severity = req.body.severity;
mate.status = req.body.status;
mate.save().then(mate => {
res.json('Update done');
}).catch(err => {
res.status(400).send('update failed');
});
}
});
});
router.route('mate/delete/:id').get((req, res) => {
mongoose.model(Mate).findByIdAndRemove({_id: req.params.id}, (err, mate) => {
if(err)
res.json(err);
else
res.json('Removed');
});
});
app.use('/', router);
app.listen(4000, () => console.log('server running on port 4000'));
//mate.js
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
let MateSchema = new Schema({
title: {
type: String
},
day: {
type: String
},
severity: {
type: String
},
status: {
type: String,
default: 'Open'
}
});
module.export = mongoose.model('Mate', MateSchema);
所以基本上,问题在于server.js中的添加控制器,该控制器必须使用创建的新json向我的数据库抛出发布请求。 好吧,它没有,但是它引发了这个错误:TypeError:Mate不是构造函数;
请帮助我弄清楚为什么会这样。我是代码新手,所以这可能是一个愚蠢的问题,但我对此深感困惑。
答案 0 :(得分:0)
使mate.js如下所示
module.exports = function(mongoose) {
var options = {
collection: 'mate',
timestamps: {
createdAt: 'created_on',
updatedAt: 'updated_on'
},
toObject: {
virtuals: true
},
toJSON: {
virtuals: true
}
};
let MateSchema = new mongoose.Schema({
title: {
type: String
},
day: {
type: String
},
severity: {
type: String
},
status: {
type: String,
default: 'Open'
}
});
return MateSchema;
};
答案 1 :(得分:0)
在模块导出中进行以下更改:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference attendanceRef = rootRef.child("Attendance");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String xname = ds.child("xname").getValue(String.class);
Log.d(TAG, xname);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
attendanceRef.addListenerForSingleValueEvent(valueEventListener);