我正在尝试更新我的活动。事件架构如下所示。我怎样才能按照自己的意愿推动价值观?我是新来的。所以,面临一些困难。提前致谢。我想在id匹配的id中发布它,并更新评论部分以及userid。
server.js
var express= require('express');
var bodyParser= require('body-parser');
var morgan = require('morgan');
var config=require('./config');
var app= express();
var mongoose=require('mongoose');
var lodash= require('lodash');
var underscore= require('underscore');
//var User=require('./database/user')
mongoose.connect('mongodb://localhost:27017/db',function(err){
if(err){
console.log(err);
}
else{
console.log("connected!");
}
});
//res.json({message:" " })
app.use(bodyParser.urlencoded({extended: true })); //if false then parse only strings
app.use(bodyParser.json());
app.use(morgan('dev'));//log all the requests to the console
var api=require('./app/routes/api')(app,express,underscore,lodash);
app.use('/api',api);
app.get('*',function(req,res){
// res.sendFile(__dirname + '/public/views/index.html');
}); // * means any route
app.listen(config.port,function(err){
if(err){
console.log(err);
}
else{
console.log("The server is running");
}
});
api.js
var User= require('../models/user');
var Event=require('../models/event');
var config=require('../../config');
var secret=config.secretKey;
module.exports=function(app,express,underscore,lodash) {
var api = express.Router();
// app.use()
api.post('/signup', function (req, res) {
var user = new User({
name: req.body.name,
username: req.body.username,
password: req.body.password
});
user.save(function (err) {
if (err) {
res.send(err);
return;
}
res.json({
message: 'User created!'
});
});
});
api.get('/users', function (req, res) {
User.find({}, function (err, users) {
if (err) {
res.send(err);
return;
}
res.json(users);
});
});
api.post('/eventfeed', function (req, res) {
var event = new Event({
name: req.body.name,
location: req.body.location,
description: req.body.description,
price: req.body.price,
rating: req.body.rating
});
event.save(function (err) {
if (err) {
res.send(err);
return;
}
res.json({
message: 'Event created!'
});
});
});
api.get('/event', function (req, res) {
Event.find({}, function (err, event) {
if (err) {
res.send(err);
return;
}
res.json(event);
});
});
api.post('/comment',function(req,res,com){
User.findByIdAndUpdate({ _id: req.body.id },
{ $push: { userid:req.body.id,comment: req.body.favourite }},
res.json(com)) ;
});
return api;
}
event.js
var mongoose= require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var EventSchema = new Schema({
name: String,
location: String,
description:String,
price: Number,
rating: Number,
reviews:[{userid:String,
comment: [String]}]
});
module.exports= mongoose.model('Event',EventSchema);
答案 0 :(得分:0)
在你的" addfavorites"控制器,使用" $ push"而不是" $ set"。这样就可以在数据库中向数组添加内容,就像在数组上常用的push函数一样。
看看,例如这里举个例子:http://blog.ocliw.com/2012/11/25/mongoose-add-to-an-existing-array/
(还有一些额外的选项" safe:true"等等,您可以使用它来确保没有覆盖)。
答案 1 :(得分:0)
您缺少$push
所以,
api.post('/addfavourites', function(req, res) {
// _id: req.body.id;
User.findByIdAndUpdate({ _id: req.body.id },
{ $push: { favourites: req.body.favourite }},
{ upsert: true },
function(fav) { res.json(fav); });
});
现在应该可以了 BTW我还在你的代码中发现了一些缺失的括号。