I tried to add mongoosastic @types with npm but that did not work, how could I make it recognize the functions? (.search() does not work either)
Error described by VS Code:
module "c:/Users/joche/Desktop/Proyectos/E-Commerce/Backend/node_modules/mongoosastic/lib/mongoosastic"
Could not find a declaration file for module 'mongoosastic'. 'c:/Users/joche/Desktop/Proyectos/E-Commerce/Backend/node_modules/mongoosastic/lib/mongoosastic.js' implicitly has an 'any' type.
Try npm install @types/mongoosastic
if it exists or add a new declaration (.d.ts) file containing declare module 'mongoosastic';
ts(7016)
Here is my code:
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const mongoosastic = require("mongoosastic");
const Schema = mongoose.Schema;
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static('public'));
mongoose.connect("mongodb://localhost:27017/gaminghard", {useNewUrlParser: true});
const ProductSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type:String, es_indexed:true },
category: String,
brand: String,
model: { type:String, es_indexed:true },
desc: { type:String, es_indexed:true },
price: Number,
stock: Number,
weight: Number,
img: String,
quantityToBuy: Number
},
{
collection: 'Products'
});
const Product = mongoose.model("Product", ProductSchema);
ProductSchema.plugin(mongoosastic, {
hosts: [
"localhost:9200"
]
});
Product.createMapping(function(err, mapping){
if(err){
console.log("error creating mapping");
console.log("err");
}else{
console.log("Mapping successfully created");
console.log(mapping);
}
});
app.listen(3000, function(){
console.log("Server started listening on port 3000");
});
Nodemon error:
C:\Users\joche\Desktop\Proyectos\E-Commerce\Backend\server.js:60
Product.createMapping(function(err, mapping){
^
TypeError: Product.createMapping is not a function
at Object.<anonymous> (C:\Users\joche\Desktop\Proyectos\E-Commerce\Backend\server.js:60:9)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
[nodemon] app crashed - waiting for file changes before starting...
package.json:
{
"name": "Backend",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "Jose Ignacio Carbone",
"license": "No license - private",
"dependencies": {
"@types/express": "^4.17.2",
"@types/mongoose": "^5.5.32",
"express": "^4.17.1",
"mongoosastic": "^4.5.1",
"mongoose": "^5.7.12",
}
}
Edit: Tried adding .d.ts file, VS Code error was gone but still couldn't compile the source code.
Self answer here, had to move
ProductSchema.plugin(mongoosastic, { hosts: [ "localhost:9200" ] });
Above
const Product = mongoose.model("Product", ProductSchema);
Because I then use Product without the plugin previously applied, which does not make sense.