Mongoose data wont save new instance of model - javascript

I have data coming in from a form, posting to /submit. My route looks like this:
var submission = require('../models/submission');
router.post('/submit', function(req,res){
var ObjectId = require('mongoose').Types.ObjectId;
var formObjId = new ObjectId(req.body.formId);
var data = new submission({
formId: formObjId,
fields: req.body.fields,
});
data.save(function (err) {
if (!err) {
//NO ERROR
} else {
return console.log(err);
}
});
res.end("Successful submission!");
});
and my submissions model:
var mongoose = require('mongoose');
module.exports = mongoose.model('Submission',{
createdAt: {type: Date, default: Date.now},
formId: [],
sourceId: [],
fields: [],
});
The first part is where I'm trying to cast the string into an object id. At the save, nothing shows up as an error, although the two fields that I'm trying to save don't save with the object. The object is saved in the database with a createdAt and object id attribute. The others are blank.
Am I doing something wrong to keep the attributes from saving with the rest of the object?

if i understand your code in right way , i found following things
Your using mongoose driver this is fine.
In the schema the data type of formId is Array .But in the response method the datatype which is assigning to this field is ObjectId.
Please check once is the fields field is exists in request body .i.e Do you sending any field which is named as 'fields' in the post request.
These are things i found when i was first look in to the code .
I hope these may helps to you .

Related

Mongoose cast to ObjectID failed for value... but why

I know what the problem is, but can't figure out why it is happening. I have a simple recipe app using express and mongoose. User passes in recipe info via form and is saved to database via mongoose methods. This part seems to work perfectly and when I console.log using test data, I see that the following data is saved:
{
ingredients: [ 'peanut butter', 'jelly', 'bread' ],
_id: 5e47d564f775ce247052d01c,
name: 'pb jelly sammich',
author: 'rob',
oneLiner: 'classic pb jelly sammich',
image: 'picofpbsammich here',
method: 'add all the ingredients together and boom! pb jelly sammich.',
__v: 0
}
(This is also what shows when I check mongo db using db.recipes.find() and also what displays when I pass in the object to my ejs show template.
However, when I access my show route via get request, I get a long error message using the above test data. Here is they key part of the error message:
'Cast to ObjectId failed for value "picofpbsammich here" at path "_id" for model "Recipes"',
I understand what the problem is, but baffled as to why it is happening. Here is my show route:
app.get("/recipes/:id", function (req, res) {
console.log(req.params.id)
Recipe.findById(req.params.id, function (err, foundRecipe) {
if (err) {
console.log(err);
} else {
res.render("show", { recipe: foundRecipe });
}
})
})
console logging the req.params.id as shown above, prints the following:
5e47d564f775ce247052d01c
picofpbsammich here
The first line is the correct ID, the second is obviously not and the cause of the problem, but I have no idea where that could be coming from :S Why would req.params.id be pulling the VALUE of a property that is named something completely different?
I'm new to mongoose so it's probably something silly I'm doing and any explanations appreciated.
Here is the model:
var mongoose = require("mongoose");
let recipeSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
author: String,
oneLiner: String,
ingredients: [String],
image: String,
method: String
})
module.exports = mongoose.model("Recipes", recipeSchema)
You posted the following code:
app.get("/recipes/:id", function (req, res) {
console.log(req.params.id)
Recipe.findById(req.params.id, function (err, foundRecipe) {
if (err) {
console.log(err);
} else {
res.render("show", { recipe: foundRecipe });
}
})
})
And you mention that in the console.log you receive:
5e47d564f775ce247052d01c
picofpbsammich here
Followed by the exception being logged:
'Cast to ObjectId failed for value "picofpbsammich here" at path "_id"
for model "Recipes"',
Makes me logically assume that you are making two requests, one of which the id is not valid, being:
picofpbsammich here
Mongoose is not able to cast this value to an ObjectId, hence you get the exception, which makes sense imo.

Reading parsed JSON into an array doesn't work?

I've been working on an application for a while and this particular feature is a part of a function which is supposed to read data from an api into an array so that I can display the contents on a webpage. Right now, I'm stuck here. Originally, I had a much longer section of code that wasn't working, but I've cut it down to a more specific problem: jsonBody.length returns 5, as expected, but articles.length returns 'undefined' and I don't understand why.
request(options, function(err, request, body) {
var jsonBody = JSON.parse(body);
var articles = new Article(jsonBody);
console.log(jsonBody.length);
console.log(articles.length);
res.render('news');
});
Thank you so much if you could help me understand. I'm not even entirely sure that I'm supposed to be using var articles at all? I can get the JSON to print to the console just find if I use jsonBody, but if I do so, I'm not sure how to utilize the contents on my 'news' page.
Here is the extended code in case you would like to see.
var marketNewsSchema = new mongoose.Schema({
datetime: String,
headline: String,
source: String,
url: String,
summary: String,
related: String,
Image: String
});
var Article = mongoose.model('MarketNews', marketNewsSchema);
app.get('/api/marketNews', function(req, res) {
var query = {
'symbol': req.body.id
};
var options = {
url: 'https://api.iextrading.com/1.0/stock/aapl/news/last/5',
method: 'GET',
qs: query
};
request(options, function(err, request, body) {
var jsonBody = JSON.parse(body);
var articles = new Article(jsonBody);
console.log(jsonBody.length);
console.log(articles.length);
res.render('news');
});
});
and the raw JSON object should be in this format:
[
{
"datetime": "2017-06-29T13:14:22-04:00",
"headline": "Voice Search Technology Creates A New Paradigm For Marketers",
"source": "Benzinga via QuoteMedia",
"url": "https://api.iextrading.com/1.0/stock/aapl/article/8348646549980454",
"summary": "<p>Voice search is likely to grow by leap and bounds, with technological advancements leading to better adoption and fueling the growth cycle, according to Lindsay Boyajian, <a href=\"http://loupventures.com/how-the-future-of-voice-search-affects-marketers-today/\">a guest contributor at Loup Ventu...",
"related": "AAPL,AMZN,GOOG,GOOGL,MSFT",
"image": "https://api.iextrading.com/1.0/stock/aapl/news-image/7594023985414148"
}
]
I think your problem is that new Article() is not an array, but you expect it to be one.
As far as I can see, Article is a mongoose schema - not an array.
So if your jsonBody is an array of articles, you might want to map over this array and generate individual articles for each object in the list.
I.e.:
var jsonBody = JSON.parse(body);
var articles = jsonBody.map(function(data) {
return new Article(data);
})
console.log(articles.length);

POSTING an array of objects to node.js/save to database using mongoose

I have a ReactJS form, in which you can dynamically add form "parts"(sections with form input). Here's an example of what I mean by "parts":
<div>
<input placeholder="Author" />
<input placeholder="Age" />
<input placeholder="Amount of books written" />
</div>
Something like this. You can add as many of these divs as you like.
I'm saving the values of these inputs in the state, which gives me a nested array like so:
this.state = {
formdata : [
{author: "somebody", age: 34, books: 0},
{author: "somebody else", age: 100, books: 1}
]
}
Right now I'm use axios post to post the data to node.js with express. This is my post function:
handleSubmit(e) {
axios.post('receivedata',
querystring.stringify({
formdata : this.state.formdata
}), {
headers : {"Content-Type": "application/x-www-form-urlencoded"}
}
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
And this is the mongoose schema that I use:
var EntrySchema = new mongoose.Schema({
formdata: [{type:String}],
updated_at: {type: Date, default: Date.now}
});
and this is how I (try to) send the data to my database:
router.post("/", function(req, res) {
newEntry.formdata = req.body.formdata;
newEntry.save(function (err) {
if(err)
res.send(err);
res.send("Entry added successfully!");
});
});
That doesn't work though and when I check the database, I receive an array with an empty string like so: formdata:[{""}]
I think the problem is with how the schema is set up, since, when I do console.log(this.state.formdata) it correctly returns the data. My next guess would be that axios is not able to send nested array, but after some research I found that that's not the case so I'm assuming that there's a special way to define nested arrays in the mongoose schema? How would I go about that?
Edit: I was thinking that maybe I could do something along the lines of:
var EntrySchema = new mongoose.Schema({
formdata: [{
author: String,
age: Number,
books: Number
}],
updated_at: {type: Date, default: Date.now}
});
I tried this and it doesn't work either. Now, I don't know if I'm on the right track or how else to do this.
I also tried changing the Content-Type in the header to "application/ json", as suggested in another answer. Once again, it didn't work.
Any help is appreciated.
Okay so after some playing around I figured it out: I used querystring.stringify() before, after changing it to JSON.stringify() it worked perfectly for me.

How i can get query string from file to query database in expressjs?

I'm trying to use this code but it's not working
var content = fs.readFileSync('/home/diegonode/Desktop/ExpressCart-master/views/partials2/menu8xz.hbs', 'utf8' );
req.db.products.find( content ).skip(0).limit(3).exec(function (err, results) {
please anyone help me (this is a extract of code)
i put the rest of code
req.db.products.find( content ).skip(0).limit(3).exec(function (err, results) {
res.render('index', {
title: 'Shop',
results: results,
session: req.session,
message: clear_session_value(req.session, "message"),
message_type: clear_session_value(req.session, "message_type" , req.body.product_id),
config: req.config.get('application'),
helpers: req.handlebars.helpers,
page_url: req.config.get('application').base_url,
show_footer: "show_footer"
});
console.log(content );
});
console.log(content );
});
It looks like your mapper's find function expects a conditions object, and when you read content directly from the file it's a plain text string that just happens to look like an object instead.
You need to do two things: first, "quote" product_title to make the file valid JSON; and second, use JSON.parse to turn content into an object which db.products.find can work with.

Backbone not making a put request with save() after save

I am experiencing a really interesting problem with backbone, I have a function like this in one of my views:
addpeople :function(){
var authArray = _.clone(this.model.get("authorizedUsers"));
var values = $(".add-input").val().split(",");
values.forEach(function(value) {
authArray.push(value);
});
this.model.set("authorizedUsers" , authArray);
this.model.save();
},
this function gets called when a button is clicked. This version of the function triggers a change event because I am cloning my array, but for some reason this.model.save()never gets called, aka the server never receives a PUT request. When I refresh the page I go back to the old state of the model..
However if I dont clone the array and change the function to, this:
addpeople :function(){
var authArray = this.model.get("authorizedUsers");
var values = $(".add-input").val().split(",");
values.forEach(function(value) {
authArray.push(value);
});
this.model.set("authorizedUsers" , authArray);
this.model.save();
},
This time the PUT request is sent successfully, but the page is not re-rendered because a change event is not triggered. When I refresh the page I can see the updated model..
I know that I can manually trigger a change event in the second example but I am more curious about why my this.model.save() is not called in the first example..
To help you understand the problem more my model looks something like:
var PostModel = Backbone.Model.extend({
urlRoot : '/tweet',
idAttribute: '_id',
defaults:{
name: '',
comments: [],
tags: [],
authorizedUsers: [],
postedBy : '',
dateCreated: ''
},
});
and my node.js update function looks like:
exports.updateTweet = function(req,res){
console.log("Getting called ! ")
var update = req.body;
var id = req.url.split("/")[2];
Post.update({ _id: id }, { $set: { authorizedUsers: req.body.authorizedUsers }}, function (err, post) {
if (err) return handleError(err);
});
res.end();
};
The reason why change didn't trigger for your second example is because it is the same object and Backbone ignore it. Hence no change event triggered.
As for why the first one failed; do you have validator for your model? May be something that validating for empty string perhaps? val() can return an empty string and split() on empty string will return [""]
Also, your defaults should be a function otherwise all your model would have the same instance of comments, tags and authorizedUsers
From Backbone doc.
Remember that in JavaScript, objects are passed by reference, so if you include an object as a default value, it will be shared among all instances. Instead, define defaults as a function.
Arrays are object too.
var PostModel = Backbone.Model.extend({
urlRoot : '/tweet',
idAttribute: '_id',
defaults: function(){
return {
name: '',
comments: [],
tags: [],
authorizedUsers: [],
postedBy : '',
dateCreated: ''
}
}
});
Lastly, array.forEach() is not available on IE8 and older.

Categories

Resources