Last Updated
Viewed 09 Times
        

I'm having an issue after passing a list of id, I use postman to test my route, enter image description here

From my controller, I map each Id then filter to search that id to the Singer that should return their equivalent value

api.post("/addBand", (req, res) => {
    let singers = req.body.singer

    let singerArr = singers.split(', ')

    let singerArrId = singerArr.map(ele => ObjectId(ele))
    let singer = Singer.find({ '_id': { $in: singerArrId } })

    const addedGroup = new GroupBand({
      bandName: req.body.bandName,
      singer: singer,
      debutDate: Date.parse(req.body.debutDate),
      createdAt: new Date()
    })
    console.log(addedGroup);


    addedGroup.save((err, group) => {
      if (err) {
        res.status(400).json({ message: err });
      }
      res.status(201).json(group);
    })
  });

Here's my model

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Singer = require('../model/singer')


const groupBandSchema = new Schema({
    bandName: {
        type: String
    },
    singer: [Singer.schema],
    debutDate: {
        type: Date
    },
    createdAt: {
        type: Date,
        default: null
    },
    updatedAt: {
        type: Date,
        default: null
    },
    deletedAt: {
        type: Date,
        default: null
    },
    assignedAt: {
        type: Date,
        default: null
    }
});

const GroupBand = mongoose.model("GroupBand", groupBandSchema);

module.exports = GroupBand;

I both use bodyParser and express.json to parse my request but still no use As a result, I only get this

{
    "createdAt": "2019-11-30T10:37:58.872Z",
    "updatedAt": null,
    "deletedAt": null,
    "assignedAt": null,
    "_id": "5de2468605761117eecb75a8",
    "bandName": "A1",
    "singer": [
        {
            "createdAt": null,
            "updatedAt": null,
            "deletedAt": null,
            "assignedAt": null,
            "_id": "5de2468605761117eecb75a9"
        }
    ],
    "debutDate": "2019-05-30T16:00:00.000Z",
    "__v": 0
}

I'm expecting an array of singers object value like this

bandName: 'Little Mix',
singer: [
 {
  _id: 1,
  name: 'Jesy'
 },
 {
  _id: 2,
  name: 'Perrie'
 },
 {
  _id: 3,
  name: 'Jade'
 },
 {
  _id: 4,
  name: 'Leigh-Anne'
 }
],
debutDate: '2011'

And the return id on that result was not even in the Singer document, I dont know if it happens to be on using body-parser? But I used that already and it works on other route. What may be the problem in here?

I pass ids in a post request in postman and reference to another model and get

5dcf8dc320742961c8b5a801, 5dcf8eca20742961c8b5a808, 5dcf8c8fef739a6159938ca8

in my controller I convert them as

let ids = [req.body.ids]
console.log(ids)

and get

['5dcf8dc320742961c8b5a801, 5dcf8eca20742961c8b5a808, 5dcf8c8fef739a6159938ca8']

I wanted to convert them to an ObjectId so I tried

ObjectId(numberArray)

and

numberArray.map(num => ObjectId(num))

but all return

(node:25080) UnhandledPromiseRejectionWarning: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

I get the following error when calling the users/status route:

{
"message": "Cast to ObjectId failed for value \"status\" at path \"_id\" for model \"Users\"",
"name": "CastError",
"stringValue": "\"status\"",
"kind": "ObjectId",
"value": "status",
"path": "_id"
}

I am using find() mongoose method to get all documents matching a criteria. Now find() does not take an _id similar to how findById does.

Whats weird is the only way I can fix it is to move my Express Router definitions around. (See Below)

This way does not work:

// users
router
 .route('/users')
 .get(getAll)
 .post(createNew);

router
 .route('/users/:id')
 .put(updateExisting)
 .get(getOne)
 .post(upload.single('proposal'), uploadFile); 

router
 .route('/users/status')
 .get(status);

This way works: (Notice how users/status and users/:id are swapped

// users
router
 .route('/users')
 .get(getAll)
 .post(createNew);

router
 .route('/users/status')
 .get(status);

router
 .route('/users/:id')
 .put(updateExisting)
 .get(getOne)
 .post(upload.single('proposal'), uploadFile);

The controller for users/status:

let status = (req,res) => {

  User.find({usersProposals: {$exists: true, $not: {$size: 0}}})
     .exec()
     .then((data) => {sendJsonResponse(res, 200, data)})
     .catch(err => {sendJsonResponse(res, 500, err)})

 };

FYI: Im running mongoose v4.13.4 and express v4.15.5

Im not sure what Im doing wrong. Seems like I'm making a stupid mistake somewhere.

I'm still getting the hang of Express/Angular and how they work together to post to the server. I'm using the MEAN stack in my sample application.

The Schema for the object I'm trying to post looks like this in Express.

First define the 'Version' Schema:

var VersionSchema = new Schema({
title: {
    type: String,
    default: '',
    trim: true
},
content: {
    type: String,
    default: '',
    trim: true
},
submitted: {
    type: Date,
    default: Date.now
},
user: {
    type: Schema.ObjectId,
    ref: 'User'
}
});

The Version Schema is used in my Draft Schema. When a user creates a draft, it becomes a Version item in the versions array. When the draft is updated, it creates a new array item so all versions of the draft are saved.

var DraftSchema = new Schema({
    versions: [VersionSchema],
});

The Draft Schema is set up as an Angular Service and injected into my controller. The service is boilerplate code for creating a $resource though so shouldn't be necessary.

I have two controllers to create the Draft object and save to Mongo: one with Angular, one with Express.

Angular controller:

$scope.create = function() {
    var version = {
        title: this.title,
        content: this.content
    };

    var draft = new Drafts({
        versions: [version]
    });

    draft.$save(function(response) {
        $location.path("drafts/" + response._id);
    });
};

In the angular controller I'm passing the title and contents to be saved to the Draft. As I understand it, you should not use an Angular controller to pass user information to the server. So this is handled by Express in the controller below.

Express controller:

exports.create = function(req, res) {
var draft = new Draft(req.body);
draft.versions = [req.user]; // Here is where my problem is, trying to save user

draft.save(function(err) {
    if (err) {
        return res.send('users/signup', {
            errors: err.errors,
            draft: draft
        });
    } else {
        res.jsonp(draft);
    }
});
};

You can see in my comment above where my problem is. I've looked through docs, SO questions, etc for the correct way to set my User property to the Versions array item being created here.

Note that if I move the User from the Version Schema to the Draft Schema and use:

draft.user = req.user;

To save the user then it works just fine. But I want the user to be saved into the VersionSchema.

To address Erick's comment below, this is a JSON representation of what I want my Draft model to look like. Versions is an Array because I want a new array item to be created each time the draft is updated.

[{
    _id: 4h55542j232555000441,
    versions: [
        {
            title: “A really great title”, 
            body: “On that day the people….”, 
            user: 5234523452349589hh23523, 
            submitted: Date 
        }
    ]
}];

Similar Question 4 (1 solutions) : Search by ObjectId in mongoose schema nodejs

Similar Question 8 (1 solutions) : Mongoose - How to find by an 'array' of ObjectId?

cc