简体   繁体   中英

Angular-Fullstack Mongoose Post into a Array

$http.post('/api/user/' + userid._id, {
    posts: {body: 'hallooo'}
});

//Doesn't work.. but

$scope.deleteThing = function(thing) {
    $http.delete('/api/things/' + thing._id);
};

Works fine. I want to Post "body" into a array and get a 404 error Here my Schema:

var UserSchema = new Schema({
    name: String,
    email: { type: String, lowercase: true },
    role: {
        type: String,
        default: 'user'
    },
    hashedPassword: String,
    provider: String,
    salt: String,
    facebook: {},
    twitter: {},
    google: {},
    github: {},
    posts: [{body: String}],
    date: { type: Date, default: Date.now }
});

What can I do?

Assuming your index.js looks like this:

'use strict';

var express = require('express');
var controller = require('./user.controller');

var router = express.Router();

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.update);
router.patch('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;

There is no route for a $http.post('api/user/' + userid._id) . If you want to update the user use $http.put('api/user/' + userid._id) .

I also suggest that you define a schema for the subdocument "posts".

something like this:

var PostSchema = new Schema({
    body: String
});

var UserSchema = new Schema({
    name: String,
    email: { type: String, lowercase: true },
    role: {
        type: String,
        default: 'user'
    },
    hashedPassword: String,
    provider: String,
    salt: String,
    facebook: {},
    twitter: {},
    google: {},
    github: {},
    posts: [PostSchema],
    date: { type: Date, default: Date.now }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM