I am learning ExpressJs tutorial from https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose
Now I have created module for mongodb database and then create a file populatedb.js in the root directory. Now I am trying to connect to database using command (as describe at last on above link)
node populatedb mongodb://dbuser:dbpassword#ds133814.mlab.com:33814/local_library_tutorial
but terminal response nothing and there doesn't happen in database
My Brother.
I ran into the same problem, and when my search led me to your question i was even more sad when I saw no answer, however after searching around, I found an answer!
Make sure you replace the "dbuser:dbpassword" placeholders with actual data e.g "Elias:js44889" on your mongo url.
Also just make sure it's also the same as the one in app.js.
I think all should be fine as it was for me.
you must include the script in populatedb.js to get work done.
populatedb.js script link : https://raw.githubusercontent.com/hamishwillee/express-locallibrary-tutorial/master/populatedb.js
After inserting the script in populatedb.js file from above link, then run the following commands to populate data in mongoDB.
npm install async (if not installed async module)
node populatedb <your mongodb url>
sometimes you may get an error if you use the same collection name in all of your models, this happened to me until i realised what i was doing was wrong.
for example don't do this
const authorModel = mongoose.model('author', AuthorSchema)
const bookModel = mongoose.model('author', BookSchema)
if you do it un-expeectedly it will throw this error
OverwriteModelError: Cannot overwrite author model once compiled.
instead in the commandline for "windows users" write
node populatedb mongodb+srv://username:password#yourcluster.mongodb.net/yourdatabase
and in your models make sure that the collections are not the same
const authorModel = mongoose.model('author', AuthorSchema)
const bookModel = mongoose.model('book', BookSchema)
I also ran into the same issue as I am doing the same tutorial. What I did to resolve this issue is go to the populatedb.js link they provide in the first step of the "Testing - create some items" or you can download it. Once I clicked on the link, they provide test data that you can paste into the populatedb.js file you've created in the root of your project then:
npm install async --save
node populatedb <your mongodb url>
Script should run and you should see the results or items as it creates them.
Followed the MDN tutorial, Here is what I fixed:
username:Jeff ; password : 12345
app.js, change to:
var mongoDB = 'mongodb://Jeff:123#ds149732.mlab.com:49732/local_library';
Command Line prompt, change to:
node populatedb mongodb://Jeff:12345#ds149732.mlab.com:49732/local_library
Posting here because this answer is the result I found googling the problem.
Had the same problem, the error ended up being the password I have set for the admin on the database - special characters were encoded (there was a warning displayed which I apparently ignored) which caused me to not be able to connect.
I had this issue for a couple of days with the error
zsh: no matches found: <my mongodb url>
First, I double checked I had followed the instructions in Testing — create some items in the tutorial.
Then I changed my original mongodb URL in the terminal, removing the end.
My original mongoURL =
mongodb+srv://username:password#cluster0.9f24o.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
My new mongoURL =
mongodb+srv://username:password#cluster0.9f24o.mongodb.net/myFirstDatabase
I ran this script while in my express-locallibrary tutorial folder.
node populatedb mongodb+srv://username:password#cluster0.9f24o.mongodb.net/myFirstDatabase
I'm not sure at this point if this means I need to change the link in app.js as well.
Hopefully this saves someone some time!
Related
I'm using #tensorflow-models/coco-ssd and #tensorflow/tfjs-node to do some object detection. It's working, but apparently could be faster. It's honestly not even that slow, it bangs through an image in about a second or two, but it just bugs me when something isn't working as well as it could.
You can find a live version of this at https://01014.org/wall-of-cats
Most current code at https://github.com/qozle/wall-of-cats
I get this on the first call to the model.detect():
============================
Hi there 👋. Looks like you are running TensorFlow.js in Node.js. To speed things up
dramatically, install our node backend, which binds to TensorFlow C++, by running
npm i #tensorflow/tfjs-node, or npm i #tensorflow/tfjs-node-gpu if you have CUDA.
Then call require('#tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of
your program. Visit https://github.com/tensorflow/tfjs-node for more details.
============================
I'm on a linux ubuntu 20 LTS server. I tried downgrading tfjs-node, I saw some folks had a problem with the versions not matching for the faceAPI example, so I tried that.
"#tensorflow-models/coco-ssd": "^2.1.0",
"#tensorflow/tfjs-node": "^2.1.0",
I tried deleting node_modules and doing
npm install
so that it would rebuild the bindings. No beans. Tried making sure I have python installed- I'm running python3. EDIT tried making sure that I have 2.7 installed instead and that I'm using it as default. No beans.
EDIT I've also tried adding #tensorflow/tfjs-backend-cpu to the mix, and rebuilding bindings again by deleting node_modules and doing npm install. No beans.
Here's some of the code:
const tf = require("#tensorflow/tfjs-node");
const cocoSsd = require("#tensorflow-models/coco-ssd");
tf.enableProdMode();
preloading the model:
catModel = await cocoSsd.load();
Then later on, when I get some data:
const image = await tf.node.decodeImage(resp.body, 3);
const predictions = await nsfwModel.classify(image);
const catObjects = await catModel.detect(image);
image.dispose();
This is for a project that interfaces with the twitter API, pulls filtered data of all posts with images that have #cat or cat or kitten in the post, checks it against a NSFW model, and then does object detection to make sure there are cats in the pictures (I got a lot of random images and couldn't really refine the twitter API filter rules).
I'm out of beans and out of ideas.
I'm using the admin-on-rest npm package starter project and trying to plug in a simple SSO Facebook login button using the FacebookAuth npm package. Every time I try to click the "Login" button, I get the following error:
FB.login() called before FB.init()
I'm using an .env file with the following variable: REACT_APP_FACEBOOK_APP_ID and setting it to the right value. I even did console.log() within my app and can see it output.
I checked and I'm only loading the FB SDK once, not multiple times (this was a common issue reported on other threads).
Ok, it turned out to be something pretty dumb, but something to point out nonetheless!
In my .env file, I had accidentally placed a semicolon (;) at the end of the declaration, like this:
REACT_APP_FACEBOOK_APP_ID = XXXXXXXXXXXX;
Apparently .env files do NOT like semi-colons. This was just very difficult to figure out from the error above.
So if any of you want to pull your hair out because of this issue, and you're using similar tech, check to make sure you're syntactically kosher EVERYWHERE variables are being declared.
the funny thing was i forgot to replace your-app-id with my app id:
<script>
FB.init({
appId: 'your-app-id',
autoLogAppEvents: true,
xfbml: true,
version: 'v8.0'
});
</script>
I am having trouble with require executing my code twice. Working on a standard Express app I build Mongoose Schemas, each in it's own files and export them.
//user.js
const User = mongoose.model('User', userSchema)
module.exports = User
//In other files
const User = require('../models/User')
Now I use this in two places in my application and get an error saying that
Cannot overwrite `User` model once compiled.
So the code above is getting called twice as it is the only code right now creating a model. However I would expect Node to only execute it once since it is required in my code.
The really strange part is that checking out an earlier version from Git I get the same error and people working with me on this get the same error. So I have no more ideas where to look for solutions.
Found the solution now.
Turns out I required the module once as models/user and once as model/User which in the cache of require creates two separate modules.
There have been many discussion about this:
one issue
another issue
old PR
It seems that this is due to Windows resolving paths case insensitive while other systems resolve paths case sensitive and node therefore doing it sensitive.
And a new module of'cause gets executed. Simply requiring is both times spelled in lowercase solved the issue.
I think the problem is in "const" that you use to declare the variable "User".
Try to use "var" instead of .
//user.js
var User = mongoose.model('User', userSchema)
module.exports = User
//In other files
var User = require('../models/User')
P/S: This is link that clarify more about "const" and "var":
Const in javascript? When to use it and is it necessary
Hope it helpful for you !
Problem: I'm getting unexpected output from code that previously worked.
Code Problem:
sails.models.user.find().then(function (users){...});
is currently returning { id: 1 }
but should return an array of User objects like [{id:x, name:y},...]
Code Alterations:
sails.models.user.find().exec(function (err, users){...}); does not contain an error and returns the same as using .then() like above.
sails.models.user.findOne(1).then(function (users){...}); correctly returns a User like {id:x, name:y}.
sails.models.venue.find().then(function (venues){...}); returns an array of venues, just as substituting any other class besides User.
Note:
This code was previously working (it's a pretty simple line), and the only changes I made between it working and not working was running npm install (but it was previously working on heroku where which installed, so I don't think that was a problem) and changing the schema of User to add a few columns (I did this by deleting the User table in the DB, updating the Sails User model, and lifting the app in create mode, so the table exactly matches the model). Neither of these should cause a problem, but we all know how "should" and coding don't mix :P
How do I fix this? And why did this happen? Thanks :)
Realized other code was calling the package sails-mock-models which was doing its job. Totally forgot about that code. Problem solved.
I would like to try running KeystoneJS without MongoDB.
There's a short blog post explaining how to do it at http://ifrederik.com/blog/2014/11/cms-without-db-running-keystonejs-without-mongodb/
Basically, it explains how to replace MondgoDB with TingoDB and using a Tungus driver.
The advice is to put the following into the top of the keystone.js file
global.TUNGUS_DB_OPTIONS = { nativeObjectID: true, searchInArray: true };
var tungus = require('tungus');
var mongoose = require('mongoose');
And later to set mongo database url to TingoDB.
keystone.set('mongo', 'tingodb://'+__dirname+'/data');
By doing this I got KeystoneJS up and running. By inspecting the contect of data/users file in TingoDB I can even see that the default user gets created, but I was not able to log in. It always reports that username / password combination is not ok.
What am I missing? How do I debug the problem to find out what exactly is the problem here?
Ok, to asnwer to myself, the problems seems to be because User.modele.findOne({email: emailRegExp}) doesn't work in TingoDB/Tungus.
When replaced it with lookup.email, without using regex-es, it semms to work.
But who knows if and what else will break because of incompatibility.