Algolia instantsearch.js Minified exception occurred; invariant - algolia

I'm getting an error from instantsearch.js that doesn't make any sense to me. The error is:
Error: Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.
I've created a fiddle with the code in question:
https://jsfiddle.net/qkqzgsv9/
Here's the HTML:
<div type="text" id="search-box" class="form-control"></div>
<table class="table table-striped">
<thead>
<tr>
<th>Number</th>
<th>Name</th>
<th>Starts</th>
<th>Duration</th>
<th>Room</th>
<th><span class="glyphicon glyphicon-info-sign"></span></th>
</tr>
</thead>
<tbody id="hits-container">
</tbody>
</table>
<div id="pagination-container"></div>
And here's the Javascript:
var search = instantsearch({
appId: '5V0BUFDX8J',
apiKey: 'a25692c12853aea7a77c5a7125498512',
indexName: 'C86FE050-6C48-11E5-84AA-BA5F164D0BA4_events',
urlSync: { useHash: true }
});
search.addWidget(
instantsearch.widgets.searchBox({
container: '#search-box',
autofocus: true,
placeholder: 'Search for events by keyword, description, or event number.'
})
);
search.addWidget(
instantsearch.widgets.hits({
container: '#hits-container',
templates: {
empty: 'No events found',
item: '<tr><td>{{event_number}}</td><td>{{name}}</td><td>{{startdaypart_name}}</td><td>{{duration_name}}</td><td>{{room_name}}</td><td><span class="glyphicon glyphicon-info-sign" data-toggle="tooltip" title="{{description}}"></span></td></tr>'
},
})
);
search.addWidget(
instantsearch.widgets.pagination({
container: '#pagination-container'
})
);
search.start();
It happens in both Safari and Chrome. And I'm not even using the minified version of instantsearch.js. Any idea what's causing it?

This is a tricky bug and it is generated becauses this widget uses React internally.
The widget tries to render a div that will contain your template. But this is incorrect because your template contains td's which can't be rendered in div's, so the browser tries to fix that. This leads to React throwing an invariant violation error because the DOM is not what it expected. And finally you can see this specific error because React is minified in the build.
The fix would be to not use td's or wait for this issue https://github.com/algolia/instantsearch.js/issues/707 to be fixed.

Related

Sails, EJS data is not defined in forEach loop

I am working on Sails.js CRUD application (article management) with MongoDB based on #bradtraversy tutorial. I have encountered issue while trying to display articles from the database using EJS syntax and forEach loop. Error says "articles data is not defined".
Error image
Basically I pass examplary record to Mongo via Sails /Create?title=ArticleOne%20body=BodyOne URL blueprint. The article can be clearly seen in Mongo. MongoDB article image
datastores.js is configured for Mongo:
module.exports.datastores = {
mongodb: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
database: 'articlebase'
},
};
routes.js points out list.ejs view:
module.exports.routes = {
'/': { view: 'pages/homepage' },
'/articles/list': { view: 'pages/list' },
'/articles/add': { view: 'pages/add' },
};
Article.js model:
module.exports = {
attributes: {
title:{
type: 'string'
},
body:{
type: 'string'
}
},
datastore: 'mongodb'
};
ArticlesController.js
module.exports = {
list:function(req, res){
Articles.find({}).exec(function(err, articles){
if(err){
res.send(500, {error: 'Database Error'});
}
res.view('list', {articles:articles});
});
},
add: function(req, res){
res.view('add');
}
};
And finally troublesome list.ejs view:
<h1 class="display-4">Articles</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th></th>
</tr>
</thead>
<tbody>
<% articles.forEach(function(article){ %>
<tr>
<td><%= article.id %></td>
<td><%= article.title %></td>
<td>
Edit
<form class="d-inline" action="/articles/delete/<%= article.id %>" method="post">
<input type="submit" value="Delete" class="btn btn-danger">
</form>
</td>
</tr>
<% }); %>
</tbody>
</table>
How can I display articles from MongoDB "articles" collection with EJS correctly? Thanks for your help in advance!
I suspect your controller method is encountering an error, but you are not returning when you get the error. My first guess would be Article vs Articles but I could be wrong. Change to this to help you see what's up:
list:function(req, res){
Articles.find({}).exec(function(err, articles){
if(err){
sails.log('Error summary: ' + err);
sails.log(err);
return res.send(500, {error: 'Database Error'}); // note the return!
}
res.view('list', {articles:articles});
});
},
Otherwise, nice work on the error tracking!
So I was getting this error repeatedly, and my fix was to check the definition of "articles".
If your ejs file is accessing the model "articles" after updating the model using jquery functions, then when you render the ejs file, you can pass the updated "articles" model as a parameter.
Like,
const articles = await Article.find().sort('-entryTime');
res.render('route/ejsfilename', { articles:articles });
This part of the code can be specified in your controller or app.js file(wherever you render your ejs).
In that way when you render the ejs file, you won't get an error of "Cannot read property 'forEach' of undefined" or "articles data is not defined".
go to config/blueprints.js, set
actions: true
blueprints will do auto route for you.

Can not edit the form from sails.js

Hey guys I'm really new at sails.js and I'm using version 1.0 of it. I just tried to create a CRUD application on adding article to my list and showing them. All goes right but when I want to delete or edit it says 404 error. I am going through this tutorial series and as I know this series is using sails js version 0.12.
I am using mongodb as my database as a default. Data insertion is going well but problem is when I wanted to edit or delete it. Here I think the problem is getting the id value from the url. I skipped the edit portion here and posted the delete portion here.
api/controllers/ ArticleController.js
module.exports = {
delete: function(req, res){
Articles.destroy({id:req.params.id}).exec(function(err){
if(err){
res.send(500,'Database Error');
}
res.redirect('/articles/list');
});
return false;
}
};
api/models/ Articles.js
module.exports = {
attributes: {
title:{
type:'string'
},
body:{
type:'string'
}
},
};
views/pages list.ejs
<h1 class="display-4">Articles</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th></th>
</tr>
</thead>
<tbody>
<% articles.forEach(function(article){ %>
<tr>
<td><%= article.id %></td>
<td><%= article.title %></td>
<td>
<form class="d-inline" action="/articles/delete/<%= article.id %>" method="POST">
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</td>
</tr>
<% }) %>
</tbody>
</table>
config/route.js
module.exports.routes = {
'/': {
view: 'pages/homepage'
},
};

algolia instantsearch.js searchBox widget indexName error

I'm getting an error using the searchBox widget from Algolia instantsearch.js The error message reads:
indexName is not valid
This error is weird, because not only does it exist, but it's displaying results! The problem repeats each time I type something into the searchBox widget, and the results are not filtered by whatever I type. I've created a plunker to see if anyone can tell me what I'm doing wrong.
http://plnkr.co/edit/ihz5VMZ6HUDaYKTqwo9V?p=preview
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//cdn.jsdelivr.net/instantsearch.js/1/instantsearch.min.css">
<script src="//cdn.jsdelivr.net/instantsearch.js/1/instantsearch.min.js"></script>
</head>
<body>
<input type="text" id="search-box" class="form-control">
<table class="table table-striped">
<thead>
<tr>
<th>Number</th>
<th>Name</th>
<th>Starts</th>
<th>Duration</th>
<th>Room</th>
<th><span class="glyphicon glyphicon-info-sign"></span></th>
</tr>
</thead>
<tbody id="hits-container">
</tbody>
</table>
<div id="pagination-container"></div>
<script>
var search = instantsearch({
appId: '5V0BUFDX8J',
apiKey: 'a25692c12853aea7a77c5a7125498512',
indexName: 'C86FE050-6C48-11E5-84AA-BA5F164D0BA4_events',
urlSync: true
});
search.addWidget(
instantsearch.widgets.searchBox({
container: '#search-box',
autofocus: true,
placeholder: 'Search for events by keyword, description, or event number.'
})
);
search.addWidget(
instantsearch.widgets.hits({
container: '#hits-container',
templates: {
empty: 'No events found',
item: '<tr><td>{{event_number}}</td><td>{{name}}</td><td>{{startdaypart_name}}</td><td>{{duration_name}}</td><td>{{room_name}}</td><td><span class="glyphicon glyphicon-info-sign" data-toggle="tooltip" title="{{description}}"></span></td></tr>'
},
})
);
search.addWidget(
instantsearch.widgets.pagination({
container: '#pagination-container'
})
);
search.start();
</script>
</body>
</html>
Ok, so it turns out that this is a bug between Safari and instantsearch.js. By changing this:
var search = instantsearch({
//...
urlSync: true
});
To this:
var search = instantsearch({
//...
urlSync: { useHash: true }
});
The error goes away.

How to use knockout mapping?

I have applied bindings to the following view model
var groupDeleteViewModel = {
group: { Id: ko.observable(), Name: ko.observable(), Members: ko.observableArray() },
load: function (item) {
debugger
},
remove: function (item) {
groupDeleteViewModel.group.Id(item.Id());
groupDeleteViewModel.group.Name(item.Name());
groupDeleteViewModel.group.Members(item.Members());
$("#groupDelete").show();
},
cancel: function () {
$("#groupDelete").hide();
}
}
the remove function loads the view with the data that has been passed in item to the remove function.
<div id="groupDelete" class="pop-window filter-view">
<h2>
Delete Group
</h2>
<table>
<tr>
<th>
Name
</th>
<td>
<span data-bind="text:group.Name" />
</td>
</tr>
<!--ko foreach: group.Members-->
<tr>
<th>
</th>
<td>
<div data-bind="text:Name" class="grey-border">
</div>
</td>
<td>
</td>
</tr>
<!--/ko-->
</table>
<span class="centeralign">
<input type="button" value="Delete" data-bind="click: remove" class="delete" />
<input type="button" value="Cancel" data-bind="click: cancel" />
</span>
</div>
I don't want to keep mapping each element of item to each element of groupDeleteViewmodel.group. I have done it at a lot of other places also in the code which has made the code really messy. I want to use the ko.mapping plugin to do the same thing.
Till now what I have tried is -
remove:function (item){
var data = ko.mapping.toJS(item);
ko.mapping.fromJS(data, groupDeleteViewModel.group);
$("#groupDelete").show();
}
But this just does not work. i really don't know why. It should have worked ideally.
Can someone tell what is the right way of using ko.mapping in such a case?
Thanks.
The mapping plugin is looking for mapping data already present in groupDeleteViewModel.group, but it doesn't find it, because you didn't initialize groupDeleteViewModel.group using the mapping plugin.
So instead of initializing group like you did:
group: { Id: ko.observable(), Name: ko.observable(), Members: ko.observableArray() }
initialize it using the mapping plugin:
group: ko.mapping.fromJS({ Id: undefined, Name: undefined, Members: [] })
And this is me fiddling around with your code: fiddle

Why is FilteringSelect in declarative dijit form causing invalid submit?

Help me understand this.
Isn't dijit.form.FilteringSelect (extended from ValidationTextBox) supposed to have property required = false by default?
Why is it that simply including a FilteringSelect in a declarative form like so below automatically results in dijit.form.Form.isValid() == false?
Even manually setting the filteringselect's required prop to false results in an invalid form submit. I feel like there's something I'm missing here.
I'm Using dojo toolkit version 1.6.1.
<!-- form.html -->
<form id="form" dojoType="dijit.form.Form">
<table>
<tr>
<td id="friend">
<select name="friend" id="friend-input" dojotype="dijit.form.FilteringSelect"></select>
</td>
</tr>
<tr>
<td>
<input type="submit" id="submit-input" value="Submit" label="Submit" dojotype="dijit.form.Button">
</td>
</tr>
</table>
</form>
/* form.js */
dojo.require("dijit.form.Button");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dijit.form.Form");
dojo.ready(function() {
var form = dijit.byId("form");
var friendInput = dijit.byId("friend-input");
friendInput.required = false;
dojo.connect(form, "onSubmit", function(event) {
event.preventDefault();
if (form.isValid()) {
alert("Ready to submit data: " + dojo.toJson(form.get("value")));
} else {
alert("Form is not valid.");
}
});
});
Like Frode mentioned, we need to set the required to false.
But, lots of fields might be used. Not a good idea to set 'required' for each, in the dojo.ready section.
<select name="friend" id="friend-input"
dojotype="dijit.form.FilteringSelect" required="false"></select>
The better way is to mention it as attribute in the html itself. Let me give an example why it is better.
If the field is included in a tab, and if the tab is refreshed on certain actions, the html will be arsed again. So, in that scenario, the required will be true again for that field. So, therefore, provide it in the html declaration of the widget itself to avoid these scenarios.