【问题标题】:"User is not authorized" on meanjsmeanjs上的“用户未授权”
【发布时间】:2015-10-27 14:33:14
【问题描述】:

我想问一下是否有人在 JSON 格式上得到相同的响应:

Objectdata:“用户未授权”标题:(名称){status:403statusText:“Forbidden”

场景:

  1. 用户 A 发布产品并对该产品添加评论。 结果:成功。

  2. 用户 B 对同一产品的评论: 结果:用户未被授权。

我用来更新产品评论的代码在这里: 应用程序名称/`

// Add comment to Product
$scope.comment = function(){            
    // console.log("name: ",$scope.user);
    // console.log("textarea: ",this.commentarea);

    var comment = {
        name: $scope.product.user.displayName,
        text: this.commentarea
    };

    $scope.product.comments.push(comment);

    $scope.product.$update(function() {
        console.log('success update');
    }, function(errorResponse) {
        console.log('success error', errorResponse);
    });

};

这是服务器端。

'use strict';
/**
 * Module dependencies.
 */
var init = require('./config/init')(),
    config = require('./config/config'),
    mongoose = require('mongoose'),
    chalk = require('chalk');

/**
 * Main application entry file.
 * Please note that the order of loading is important.
 */

// Bootstrap db connection
var db = mongoose.connect(config.db, function(err) {
    if (err) {
        console.error(chalk.red('Could not connect to MongoDB!'));
        console.log(chalk.red(err));
    }
});

// Init the express application
var app = require('./config/express')(db);

// Bootstrap passport config
require('./config/passport')();

// Start the app by listening on <port>
app.listen(config.port);

// Expose app
exports = module.exports = app;

// Logging initialization
console.log('MEAN.JS application started on port ' + config.port);

【问题讨论】:

    标签: angularjs mongodb mongoose meanjs


    【解决方案1】:

    如果您的 Products 架构如下所示:

    var ProductSchema = new Schema({
        created: {
            type: Date,
            default: Date.now
        },
        title: {
            type: String,
            default: '',
            trim: true,
            required: 'Title cannot be blank'
        },
        comments: [{
            type: String,
            default: '',
            trim: true
        }]
    });
    

    您已经在app/routes/products.server.routes.js 文件中限制了您的产品路线,如下所示:

    app.route('/products/:productId')
        .get(products.read)
        .put(users.requiresLogin, products.hasAuthorization, products.update)
        .delete(users.requiresLogin, products.hasAuthorization, products.delete);
    

    然后非授权用户无法添加评论,因为他们无法更新产品记录。

    您可能想要创建一个单独的 CommentsSchema 并使用 Mongoose ObjectId 类型来创建与产品的一对多关系:

    var CommentSchema = new Schema({ 
        product: ObjectId,
        content: {
            type: String,
            default: '',
            trim: true,
            required: 'Content cannot be blank'
        }, 
    })
    

    这将保护您的产品的安全性并允许未经授权的用户发表评论,但需要您执行稍微复杂的查询才能让您的 cmets 出现在您的产品视图中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-19
      • 2011-12-26
      • 2017-06-12
      • 2015-10-21
      • 1970-01-01
      • 2019-03-07
      • 2019-04-11
      • 2018-11-19
      相关资源
      最近更新 更多