我无法使用Restangular更新Mongodb数据库中的现有文档(myParams
)并使用新对象(new Thread(new Runnable(){
public void run(){
mScrollView.fullScroll(View.FOCUS_UP);
}
})).start();
。到目前为止,我可以在前端添加评论,没有任何问题,但我无法将评论详细信息发布到我的数据库。目前,当我提交新的products
时,我的代码会在我的reviews
集合中创建一个新密钥,但不保存审核的详细信息。我如何将评论推送到服务器?如果我需要提供任何其他详细信息或说明,请告知我们。任何帮助将不胜感激。
产品的样本JSON
review
添加评论后,这是我的新评论的JSON输出
products
这是我期望在我的JSON输出中看到的
{"_id":"xxxxx","name":"Product 1","description":"Product 1 Description","price":"1299.99","createdOn":"143767117903", "reviews":[{}]}
项目详情
我使用了Yeoman角度生成器,因此我有一个{"__v":0,"_id":"xxxxxx"}
和一个{"__v":0,"_id":"xxxxxx","stars":4,"body":"Test review","author":"example@domain.com","createdOn":143767117903}
目录。我使用的是server
,client
,MongoDB
,MongooseJS
和ExpressJS
。据我所知,我的产品的服务器路由正常,因为我能够查看所有产品,添加产品,查看产品(包括与产品相关的任何评论),并至少添加空白评论。
我有AngularJS
架构,其中包含NodeJS
架构的嵌入式文档。
产品架构
products
评论架构
reviews
产品评论控制器
/**
* Schema for Products
*/
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var productSchema = new Schema({
name: {
type: String,
require: true,
},
description: {
type: String,
require: true,
},
shine: {
type: Number,
require: true,
},
price: {
type: Number,
require: true,
},
rarity: {
type: Number,
require: true,
},
color: {
type: String,
require: true,
},
faces: {
type: Number,
require: true,
},
images: {},
reviews: [{type: mongoose.Schema.Types.ObjectId, ref: 'Review'}],
createdOn: {
type: Date
}
});
var Product = mongoose.model('Product', productSchema);
module.exports = Product;
产品服务
/**
* Schema for Product Reviews
*/
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var reviewSchema = new Schema({
stars: {
type: Number
},
review: {
type: String
},
author: {
type: String
},
createdOn: {
type: Date
}
});
var Review = mongoose.model('Review', reviewSchema);
module.exports = Review;
答案 0 :(得分:1)
The solution which was provided by a co-worker is to edit the server routes for my product/reviews routed.
updated products review controller
angular.module('gemStoreApp')
.controller('ReviewCtrl', ["$scope, ,'$resource', '$Restangular', 'productsService', function ($scope, $resource, Restangular, productsService) {
this.review = {};
this.addReview = function(product){
this.review.createdOn = Date.now();
var productReview = Restangular.all('/products/' + product._id + '/reviews');
var updatedProduct = product;
productReview.post(this.review).then(function(newResource){
product.reviews.push(newResource);
});
this.review = {};
};
}]);
updated reviews route
...
router.post('/:product/reviews', function (req, res, next) {
var time = moment().formated('MMMM Do YYYY, h:mm:ss a');
var body = req.review;
var review = new Review(req.body);
review.product = req.product
review.save(function (err, review){
if (err) {return next(err);}
req.product.reviews.push(review);
req.products.save(function (err, product) {
if (err) {return next(err);}
res.json(review);
res.status(201).json({
'message': review.review + 'created'
});
});
});
...
I will also post the direct URL to the files on my GitHub project once I commit my changes.