Mongoose: moment().format not working - node.js

Im having Mongoose schema as follows:
createdOn: {
type: String,
//default: Date.now,
default: moment(new Date(Date.now())).format('MMM Do YY') //npm install moment --save # npm
},
updatedOn: {
type: String,
//default: Date.now
default: moment(new Date(Date.now())).format('MMM Do YY')
}
UPDATE:
After changing the type to String,
I get :
Fri Jul 28 2017 14:43:40 GMT+0530 (IST)
How to remove the time and SMT.
I need to keep only Fri Jul 28 2017
I installed Moment package of node js (http://momentjs.com/)
Whats wrong in my schema above:
I get
MongooseError: Cast to date failed for value "Aug 7th 17" at path "updatedOn"
message: 'Cast to date failed for value "Aug 7th 17" at path "updatedOn"',
name: 'CastError',
stringValue: '"Aug 7th 17"',
kind: 'date',
value: 'Aug 7th 17',
path: 'updatedOn',
reason: undefined }

The double default assignment looks wrong.
default: default: moment(new Date(Date.now())).format('MMM Do YY')
As a better practice, you should consider just using UNIX timestamps rather than formatted date strings. Timestamps will allow you to easily track statistics on the database.
So you could just use the:
Date.now() // returns a UNIX timestamp
In moment.js a UNIX timestamp:
moment().unix()
If for some reason you need the timestamp in a string format you could always just do:
moment().unix() + ''

Related

How do I format [future, non-current] dates in Eleventy + Nunjucks?

I'm building a site with a CMS (Netlify) for a local band, and they have future gig dates they will put on the site. So far the dates show up as very long non-formatted strings that include the time and time zone. I'm trying to figure out how to format the dates to be simpler (day, date, time for example).
I've tried plugins like nunjucks-date but I'm a little confused about how to use a plugin (and filters) in this case.
My repo: https://github.com/mollycarroll/serapis-eleventy-2
Example gig entry:
---
layout: gig
venue: Cedar Lake Cellars
date: 2022-05-28
time: 6pm
city: Wright City, MO
---
Gig template:
<h2>{{ venue }}</h2>
<h4>{{ city }} {{ date }} {{ time }}</h4>
config.yml for the CMS:
- name: 'gigs'
label: 'Shows'
folder: 'src/gigs'
create: true
slug: '{{month}}-{{day}}-{{venue}}'
fields:
- { label: 'Layout', name: 'layout', widget: 'hidden', default: '_includes/gig.njk' }
- { label: 'Date', name: 'date', widget: 'date', default: '' }
- { label: 'Time', name: 'time', widget: 'string', default: '' }
- { label: 'Venue', name: 'venue', widget: 'string', default: '' }
- { label: 'City', name: 'city', widget: 'string', default: '' }
Thanks for any help.
First, you should create a filter, let's say src/filters/date.js with the following content:
const { DateTime } = require("luxon");
// Add a friendly date filter to nunjucks.
// Defaults to format of LLLL d, y unless an
// alternate is passed as a parameter.
// {{ date | friendlyDate('OPTIONAL FORMAT STRING') }}
// List of supported tokens: https://moment.github.io/luxon/docs/manual/formatting.html#table-of-tokens
module.exports = (dateObj, format = 'LLLL d, y') => {
return DateTime.fromISO(dateObj, { zone: "Europe/Amsterdam", locale: "en" }).toFormat(format);
};
Make sure you check Luxon documentation for details. Then add the filter in .eleventy.js:
module.exports = function(eleventyConfig) {
...
eleventyConfig.addFilter("date", require("./src/filters/date.js"));
...
};
Now you can use it in Nunjacks with a default value {{ date }}, in this example 'LLLL d, y', or any value you need at a certain position on your website {{ date | date('dd. LLLL yyyy.') }}. This can be very useful if you need at some point just month and year or just day and month.
You can even create multiple language filters, like dateEn.js and dateDe.js, and format each to its own language if you have a multilingual site.
Hope this helps.
EDIT: In order for this filter to work the dateObj should be in ISO 8601 format.

How to get historical stock data between two dates?

Im trying to get historical data for Apple from google finance.
but the start and end time is always the same.
in google-finance module sending a get request with these params to http://www.google.com/finance/historical should result in historical data between the dates but it returns last year instead.
var params = { q: 'AAPL',
startdate: '2014-01-01',
enddate: '2014-12-31',
output: 'csv'
};
google-finance module example :
gFinance.historical({
symbol: 'NASDAQ:AAPL',
from: '2014-01-01',
to: '2014-12-31'
}, function (err, quotes) {
console.log(quotes);
});
result is always from today (8 oct 2017) to one year earlier(8 oct 2016):
...
{ date: 2017-02-22T20:30:00.000Z,
open: 137.38,
high: 137.48,
low: 136.3,
close: 136.53,
volume: 20788186,
symbol: 'NASDAQ:AAPL' },
{ date: 2017-02-23T20:30:00.000Z,
open: 135.91,
high: 136.66,
low: 135.28,
close: 136.66,
volume: 21776585,
symbol: 'NASDAQ:AAPL' },
{ date: 2017-02-26T20:30:00.000Z,
open: 137.14,
high: 137.44,
low: 136.28,
close: 136.93,
volume: 20257426,
symbol: 'NASDAQ:AAPL' },
{ date: 2017-02-27T20:30:00.000Z,
open: 137.08,
high: 137.44,
low: 136.7,
close: 136.99,
volume: 23482860,
symbol: 'NASDAQ:AAPL' },
{ date: 2017-02-28T20:30:00.000Z,
open: 137.89,
high: 140.15,
low: 137.6,
close: 139.79,
volume: 36414585,
symbol: 'NASDAQ:AAPL' },
{ date: 2017-03-01T20:30:00.000Z,
open: 140,
high: 140.28,
low: 138.76,
close: 138.96,
volume: 26210984,
symbol: 'NASDAQ:AAPL' },
{ date: 2017-03-02T20:30:00.000Z,
open: 138.78,
high: 139.83,
low: 138.59,
close: 139.78,
volume: 21571121,
symbol: 'NASDAQ:AAPL' },
... 151 more items ]
I dont know if they changed something or they closed it like yahoo did or Im doing something wrong.
I searched for a week and everything seems outdated.
how can I get historical data between custom times with http request?
Gogle finance works as expected :
http://finance.google.com/finance/historical?q=AAPL&startdate=2014-01-01&enddate=2014-12-31&output=csv
You didn't put any source code.

Second if statement in jade layout not working

The first if statement is firing corretly and marking the checkbox as checked when the value is true, but the second isn't.
- var profileVal = false
- var logbookVal = false
div.form-group.col-md-6
-if(user.profilePublic){
- profileVal = true
-}
label Public profile
p Would you like your profile to be public?
input(type='checkbox',name='userPublicProfile',class='floatLeftCheckbox',checked=profileVal)
div.form-group.col-md-6.clearfix
label Public logbook
p Would you like your logbook to be public?
-if(user.logPublic){
- logbookVal = true
-}
input(type='checkbox',name='userPublicLogbook',class='floatLeftCheckbox',checked=logbookVal)
Both are set to Boolean in mongoDB. Both are correctly spelled etc.
Result of console.log:
deserializing user: { _id: 5547e77928d405cc236d4a01,
updated_at: 'Mon May 04 2015 17:41:13 GMT-0400 (EDT)',
created_at: 'Mon May 04 2015 17:41:13 GMT-0400 (EDT)',
admin: false,
lastName: '######',
firstName: '######',
email: '######',
password: '######',
callsign: '######',
uid: '$2a$10$/yF1pqR9N5VkXcnLaNDPBea8xH.tO3Tto82W.9wGD3VdZGPAdXID6',
profilePublic: true,
logPublic: true,
__v: 0 }
Also tried:
-if(user.logPublic)
p It's true
Still nothing

Sails-mongo many to many association not working

I am using sails 0.10.5 , and i have two models as below for many to many relation. but i am getting error while lifting app.
model 1) Category.js
attributes: {
name: {
type: 'string',
required: true
},
features: {
type: 'array'
},
desc: {
type: 'string'
},
sub_cats: {
collection: 'SubCategory',
via: 'cats',
dominant: true
}
}
model 2) SubCategory.js
attributes: {
cats: {
collection: 'Category',
via: 'sub_cats'
},
name: {
type: 'string',
required: true
},
features: {
type: 'array'
},
desc: {
type: 'string'
}
}
Now while lifting sails app, this error always trigger
"C:\Program Files (x86)\JetBrains\PhpStorm 8.0.2\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" app.js
c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\utils.js:47
return hop.call(obj, prop);
^
TypeError: Cannot convert null to object
at hasOwnProperty (native)
at exports.object.hasOwnProperty (c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\utils.js:47:14)
at JoinTables.parseAttribute (c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\joinTables.js:148:26)
at c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\joinTables.js:83:22
at Array.forEach (native)
So i am confused (my code is wrong or there is some problem in sails-mongo many-many relation)
--ND
It is just a typo in SubCategory.js: It should be attributes instead of attribute.
Hopefully you've solved your problem by now! For others like me who ran into this error, balderdashy/waterline-schema just released a more helpful error message at https://github.com/balderdashy/waterline-schema/issues/17 which should help you figure out your particular problem. Because as of today (2015-04-28) npm install does not get you this most recent version, you can manually fix it by doing the following:
Navigate to node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema (note: this may be in your global sails directory)
Replace joinTables.js with https://github.com/balderdashy/waterline-schema/blob/8f5a44d076ca36edea409bf41ac2cdbf760c2c59/lib/waterline-schema/joinTables.js
I know this doesn't exactly answer your question as to what went wrong, but hopefully it will give others with that error the tools to solve it more quickly.

Rails Mongoid search parents elements that have at least one child

I have a rails 4 app and I have 2 objects: stores and books (referenced 1:n relation).
A store has many books, each book belongs to one store. Some stores don't have any books.
How can I make a query to find the 3 latest store that has at least 1 book - and the 3 latest stores that have no books ?
#stores = Store.order_by(:created_at => 'desc').limit(4).uniq
#books = Book.order_by(:created_at => 'desc').limit(4).uniq
This work but I don't know how to do a where(book.exists?) for store, or for the #books to make sure each book belongs to a unique store.
Basic CRUD operations in MongoDB operate on a single collection,
"join" capability is not yet implemented that would allow them to operate across two collections.
Once you understand this, you can easily construct two operations that will do what you want.
The following is the most obvious given the schema hints that you supplied.
To do anything more, you probably want to consider schema changes like embedding.
By definition, your supplied schema only allows a book to belong to one (unique by definition) store.
You can see this from the inspection of the books, that there is a single value store_id for each book.
Hope that this helps your understanding.
app/models/store.rb
class Store
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
has_many :books
end
app/models/book.rb
class Book
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
belongs_to :store
end
test/unit/store_test.rb
require 'test_helper'
require 'pp'
class StoreTest < ActiveSupport::TestCase
def setup
Mongoid.default_session.drop
end
test '0. mongoid version' do
puts "\nMongoid::VERSION:#{Mongoid::VERSION}\nMoped::VERSION:#{Moped::VERSION}"
end
test 'store query has book, does not have book' do
[
["Amazon.com", ["Outlander", "Taking It All"]],
["Barnes & Noble", ["Big Little Lies"]],
["Goodreads", []],
["Greenlight Bookstore", []],
["Powell's Books", ["Gone Girl", "Dark Skye"]],
["Strand Books", []]
].each do |store, books|
store = Store.create(name: store)
books.each do |title|
store.books << Book.create(title: title)
end
sleep 1
end
assert_equal(6, Store.count)
assert_equal(5, Book.count)
puts
store_ids_with_books = Book.distinct(:store_id)
latest_stores_with_a_book = Store.in(_id: store_ids_with_books).order_by(:created_at => 'desc').limit(3).to_a
puts "three latest stores with a book:"
pp latest_stores_with_a_book
latest_stores_without_a_book = Store.nin(_id: store_ids_with_books).order_by(:created_at => 'desc').limit(3).to_a
puts "three latest stores without a book:"
pp latest_stores_without_a_book
puts "books:"
pp Book.all.to_a
end
end
rake test
Run options:
# Running tests:
[1/2] StoreTest#test_0._mongoid_version
Mongoid::VERSION:3.1.6
Moped::VERSION:1.5.2
[2/2] StoreTest#test_store_query_has_book,_does_not_have_book
three latest stores with a book:
[#<Store _id: 53f257287f11ba75e5000008, created_at: 2014-08-18 19:42:32 UTC, updated_at: 2014-08-18 19:42:32 UTC, name: "Powell's Books">,
#<Store _id: 53f257257f11ba75e5000004, created_at: 2014-08-18 19:42:29 UTC, updated_at: 2014-08-18 19:42:29 UTC, name: "Barnes & Noble">,
#<Store _id: 53f257247f11ba75e5000001, created_at: 2014-08-18 19:42:27 UTC, updated_at: 2014-08-18 19:42:27 UTC, name: "Amazon.com">]
three latest stores without a book:
[#<Store _id: 53f257297f11ba75e500000b, created_at: 2014-08-18 19:42:33 UTC, updated_at: 2014-08-18 19:42:33 UTC, name: "Strand Books">,
#<Store _id: 53f257277f11ba75e5000007, created_at: 2014-08-18 19:42:31 UTC, updated_at: 2014-08-18 19:42:31 UTC, name: "Greenlight Bookstore">,
#<Store _id: 53f257267f11ba75e5000006, created_at: 2014-08-18 19:42:30 UTC, updated_at: 2014-08-18 19:42:30 UTC, name: "Goodreads">]
books:
[#<Book _id: 53f257247f11ba75e5000002, created_at: 2014-08-18 19:42:28 UTC, updated_at: 2014-08-18 19:42:28 UTC, title: "Outlander", store_id: "53f257247f11ba75e5000001">,
#<Book _id: 53f257247f11ba75e5000003, created_at: 2014-08-18 19:42:28 UTC, updated_at: 2014-08-18 19:42:28 UTC, title: "Taking It All", store_id: "53f257247f11ba75e5000001">,
#<Book _id: 53f257257f11ba75e5000005, created_at: 2014-08-18 19:42:29 UTC, updated_at: 2014-08-18 19:42:29 UTC, title: "Big Little Lies", store_id: "53f257257f11ba75e5000004">,
#<Book _id: 53f257287f11ba75e5000009, created_at: 2014-08-18 19:42:32 UTC, updated_at: 2014-08-18 19:42:32 UTC, title: "Gone Girl", store_id: "53f257287f11ba75e5000008">,
#<Book _id: 53f257287f11ba75e500000a, created_at: 2014-08-18 19:42:32 UTC, updated_at: 2014-08-18 19:42:32 UTC, title: "Dark Skye", store_id: "53f257287f11ba75e5000008">]
Finished tests in 6.193234s, 0.3229 tests/s, 0.3229 assertions/s.
2 tests, 2 assertions, 0 failures, 0 errors, 0 skips

Resources