This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
Probably missing something simple here but cant for the life of me figure out why the below function is returning undefined.
var isOrphanEan = function isOrphanEan (ean) {
Products.findOne({
'ean': ean
}, function (err, product) {
return product.orphan;
});
}
isOrphanEan(12345); //returns undefined
example of product
{
_id: 55ad6b442afbebe82d077a04,
orphan: true
}
edit:
console.logging product returns:
{ _id: 55ad6b442afbebe82d077a04,
ean: 4006643097328,
aw_product_id: 3295182687,
product_name: 'Multipower Shaker Neutral',
product_brand: 'Multipower UK',
product_description: '',
img_sml: 'http://images.productserve.com/noimage.gif',
img_lrg: '',
rating: '',
merchant_id: 2926,
price_current: 'GBP4.99',
price_rrp: '',
aff_link: 'http://www.awin1.com/pclick.php?p=3295182687&a=234945&m=2926',
direct_link: 'http://www.multipower.com/uk/product/multipower-shaker-09732/neutral',
merchant_product_id: '09732',
aw_image_url: '',
orphan: true,
created_at: Mon Jul 20 2015 22:42:28 GMT+0100 (BST),
updated_at: Thu Oct 08 2015 23:20:35 GMT+0100 (BST),
__v: 0 }
Use callbacks approach to cope with async responses:
var isOrphanEan = function isOrphanEan (ean, cb) {
Products.findOne({
'ean': ean
}, cb);
}
isOrphanEan(12345, function(err, product) {
console.log(product.orphan);
});
You are using a callback. Function isOrphanEan() does not return anything, rather findOne() will call the callback when the data becomes available. You need to process you product.orphan in the unnamed callback.
Related
I'm new to mongoose. I'm trying to query by createdAt date, with startDate and endDate, however I got the incorrect number of results.
data
{"_id":{"$oid":"5f4fab9beceaa20f898feafb"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Alex","email":"alex#gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-02T14:26:35.237Z"},"updatedAt":{"$date":"2020-09-02T14:26:35.237Z"}}
{"_id":{"$oid":"5f4fc3677e7b1e2d806714cf"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Joshua","email":"joshua#gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-02T16:08:07.123Z"},"updatedAt":{"$date":"2020-09-02T16:08:07.123Z"}}
{"_id":{"$oid":"5f50b80f28ca26065b2ac9a5"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Harold","email":"harold#gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-03T09:31:59.112Z"},"updatedAt":{"$date":"2020-09-03T09:31:59.112Z"}}
{"_id":{"$oid":"5f59104ff518c40579b578d0"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Katy","email":"katy#gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-09T17:26:39.787Z"},"updatedAt":{"$date":"2020-09-09T17:26:39.787Z"}}
I have 4 records with the ff. date 2020-09-02, 2020-09-03 and 2020-09-09
I wanted to get all records from 2020-09-02 and 2020-09-03, with these I expected 3 results as I have to records on the 2020-09-02, however I only got 2 results, those records have 2020-09-02 date with them.
const { limit = 30 } = params;
return new Promise((resolve, reject) => {
const query = {
createdAt: {
$gte: '2020-09-02',
$lte: '2020-09-03',
}
};
this.model.find(query).sort({
createdAt: 'descending',
}).limit(limit).exec((err, res) => {
if (!err) {
resolve(res);
}
reject(err);
})
})
Did I miss something with my code?
I also tried passing new Date('2020-09-02') but I still got same results.
I tried setting mongoose debug to true and below is what I got.
Mongoose: inquiries.find({ createdAt: { '$gte': new Date("Wed, 02
Sep 2020 00:00:00 GMT"), '$lte': new Date("Thu, 03 Sep 2020 00:00:00 GMT") }}, { sort: { createdAt: -1 }, limit: 30, projection: {}
})
Thanks in advance.
Youre looking for records greater than 2020-09-02 00:00:00 and less than 2020-09-03 00:00:00.
You only have 2 records which are between these values, if you want records including those at 2020-09-03 23:59:59, set your lte to 2020-09-04
I'm using a snapshot test in my project and came across a weird problem when running this specific test on a CI server: it displays the timezone name instead of the GMT code, causing the test failure.
I have tried using "moment-timezone" and Date.UTC() to normalize the dates, the result shown was the correct date with the same issue as above.
I've also tried to stub the global.Date object, but the components complained about prop incompatibility.
it('should render with props', () => {
const order = {
merchant: { logo: 'abc', name: 'Pizza Hut' },
bag: {
items: [{ name: 'Corn & Bacon' }],
total: {
valueWithDiscount: 99.99,
},
},
delivery: {
deliversAt: new Date('2019-05-21 13:00'),
},
payment: {
mode: 'online',
},
lastStatus: API_STATUSES.cancelled,
createdAt: new Date('2019-05-21 12:00'),
details: {},
};
const wrapper = shallowMount(Order, {
...commons,
propsData: { order },
});
expect(wrapper).toMatchSnapshot();
});
See that the expected date is the same as the received one, but syntactic differences:
<div class="order__details">
- <orderdetails-stub paymentmode="online" deliverytime="Fri Jun 21 2019 10:00:00 GMT-0300 (GMT-03:00)" value="99.99" laststatus="cancelled"></orderdetails-stub>
+ <orderdetails-stub paymentmode="online" deliverytime="Fri Jun 21 2019 10:00:00 GMT-0300 (Brasilia Standard Time)" value="99.99" laststatus="cancelled"></orderdetails-stub>
Using Date strings as props like this is hazardous and likely to lead to the sort of problem you're encountering.
Best practice for tests in my experience is to use Date.getTime() so values are numbers of milliseconds without any locale information.
Alternatively, you can use moment-timezone as described in this article:
import moment from 'moment-timezone';
it('renders without crashing', () => {
moment.tz.setDefault('EST');
let props = {
currentDay: moment("2017-09-15 09:30:00").format("MMM Do YYYY h:mm:ss a")
};
const tree = renderer.create(<App {...props} />).toJSON();
expect(tree).toMatchSnapshot();
});
It's to my understanding that there is no .toObect() in JavaScript but it is used in mongoose to change the mongoose documents to an object so you can use JavaScript built in functions.
I don't have a grasp on when to use it. sometimes when i get an array of docs I could use forEach on the returned array and then other times I would spend 20 minutes working out why the forEach doesn't work. then I would add .toObject to the returned Array and the forEach would work. something like that. I'm not sure about my memory if it was forEach or something else.
Anyways this is the latest weird issue. I'm in EJS and I needed to do <% console.log("typeof", user.toObject().hasOwnProperty("facebook")) %> to work instead of <% console.log("typeof", user.hasOwnProperty("facebook")) %> . The one with the .toObject() consoles : typeof true the one without consoles typeof false . That seems weird to me. Is user a mongoose document? How could I use .toObject in ejs? Oh wait a minute. Im just thinking is it because it is in the "<%%>" it gets connected to the server side code and maybe I have mongoose required in my server.js Why did I have to use toObject to get the true value?
any ways I didn't think I needed to use .toObject()
This is what I have in my .js file : res.render("editProfile", {aboutUser : returnedUser, user : req.user}); I think req.user is from passport not mongoose.
user obj
{ _id: 581a2chan3changed727, defaultImage: 'https://scontefrfrxxffbcdn.net/v/t1.0-1/s148x148/29731_575634691740_1085883_n.jpg?oh=797bc81addf3611309changedoe=588AC839', urlname: 'Jacchanged', momented: 'Nov 2, 2016', username: 'Jack Schuldenfrei', __v: 0, usefulness: [], createdOn: Wed Nov 02 2016 14:15:47 GMT-0400 (Eastern Daylight Time), shortId: 'rkn8powgl', pointsRecord: [], repPoints: 0, facebook: { posts: '{"email":"jschuldenfreiGTGTGTgchanged,"name":"Jack Schuchange","gender":"male","picture":{"data":{"height":148,"is_silhouette":false,"url":"https:\\/\\frfr.net\\/v\\/t1.0-1\\/s148x148\\/29731_575634691740_1fr5883_n.jpg?oh=797bc81addf36113e0933a67eef32ab9&oe=588AC839","width":93}},"id":"10100584555845400"}', gender: 'male', email: 'jschuldenfredu', name: 'Jack changes', token: 'EAAPs693O1UMBAHwTcyZAPtRiXrwvxrBWhGCJZCQlAIfHRZAiCLY3tYDVoviB4yDrK68WrsUnuxlcHfUJE984aAvWOnFZASqbUjYZAhHnsL0mFCZCNRQwsn3oJn1acu1qnSPFko6I3ShZAtPIMumrVlpVxR0ZD', id: '1010058386584400' }, reviews: [] } { _id: 581a2d53380e4c70ac728, userId: 581a2d380e4c7037aac727, username: 'Jack changed', urlname: 'Jachanged', __v: 0, question1: { name: 'This is for question1' } }
I get that user object by doing <%=user%> in the ejs file.
What you want is .lean()
Usually passport has a serialization configuration that looks like
// Serialize sessions
passport.serializeUser(function (user, done) {
done(null, user.id);
});
// Deserialize sessions
passport.deserializeUser(function (id, done) {
User.findOne({
_id: id
}, '-salt -password', function (err, user) {
done(err, user);
});
});
Change the deserialization process like
// Deserialize sessions
passport.deserializeUser(function (id, done) {
User.findOne({
_id: id
}, '-salt -password')
.lean() // lean converts mongoose.Document to Plain Javascript Object
.exec(function (err, user) {
console.log(user instanceof mongoose.Document) // false
done(err, user);
});
});
I am processing data read from database on the server using the following code:
module.exports = mongoose.model('Todo', {
text : {type : String, default: ''},
created_at : Date
});
var processTodos = function ( todos ){
for (var i = todos.length - 1; i >= 0; i--) {
// Following update is not happening
todos[i].created_at = "Custom date";
};
console.dir(todos);
return todos;
};
I am not able to figure out how to update this. Is there a syntax issue that is causing this.
I am using MEAN stack for my application.
// Following update is not happening
todos[i].created_at = "Custom date";
What am i missing here.
Here is the console log for "console.dir(todos);":
{ _id: 5489dda3f23f159400475dba,
created_at: Thu Dec 11 2014 23:38:35 GMT+0530 (India Standard Time),
__v: 0,
text: 'Testing sorting at server side' }
{ _id: 5489ddacf23f159400475dbb,
created_at: Thu Dec 11 2014 23:38:44 GMT+0530 (India Standard Time),
__v: 0,
text: 'It works' }
{ _id: 5489f31a12fa54cc127f3e1d,
created_at: Fri Dec 12 2014 01:10:10 GMT+0530 (India Standard Time),
__v: 0,
text: 'time to add more data' }
If you'd like to save the changes you're making to your object, you need to persist the change using the .save() method like so:
var processTodos = function ( todos ){
for (var i = todos.length - 1; i >= 0; i--) {
// Following update is not happening
todos[i].created_at = "Custom date";
todos[i].save();
};
console.dir(todos);
return todos;
};
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can’t you modify the data returned by a Mongoose Query (ex: findById)
First I am making the query to mongoDB, get all the correct results but only the small modification to object literal does not work. What I am trying to do, is adding new field to comments. I tried to use the DBref method but it didn't work so i make 2 queries now.
var query = Rss.findOne({ _id: itemId});
query.exec(function(err, feed) {
if (!err && feed.comments) {
console.log(feed.comments.length);
for (var index in feed.comments) {
var author = feed.comments[index].author;
if (author !== undefined) {
User.findById(author, function(err, user) {
/**Problem is here **/
feed.comments[index].name = 'Some random field';
console.log('Added new field' + util.inspect(feed));
});
}
}
}
});
Also the response is this without the missing .name field.
Added new field{ _id: 4f34f343c7b0434217000012,
original_link: 'http://com',
publish_date: Fri, 10 Feb 2012 10:36:00 GMT,
summary: 'some text',
title: 'title exampel',
comments:
[ { body: 'well',
author: 4f30265e6f60dc061d000002,
_id: 4f34f3b6f96c58541700000f,
create_date: Fri, 10 Feb 2012 10:38:46 GMT } ],
create_date: Fri, 10 Feb 2012 10:36:51 GMT }
// EDIT more information
Well i haven't found the answer but some how the console.log(feed.comments[index]) returns reference to function. Maybe someone who has more experience with mongoosejs could explain what would be workaround in this situation.
{ [Function] author: 'Some random field' }
You have to tell Mongoose to convert the result into a proper object. Before modifying the feed object simply call:
feed = feed.toObject();
And then you can add all the additional properties you want to it.