不能使用 mongoosastic 函数 .x 不是函数
Cannot use mongoosastic functions .x is not a function
我尝试用 npm 添加 mongoosastic @types 但是没有用,我怎样才能让它识别函数? (.search() 也不起作用)
VS Code 描述的错误:
模块"c:/Users/joche/Desktop/Proyectos/E-Commerce/Backend/node_modules/mongoosastic/lib/mongoosastic"
找不到模块 'mongoosastic' 的声明文件。 'c:/Users/joche/Desktop/Proyectos/E-Commerce/Backend/node_modules/mongoosastic/lib/mongoosastic.js' 隐式具有 'any' 类型。
如果存在,请尝试 npm install @types/mongoosastic
或添加包含 declare module 'mongoosastic';
ts(7016)
的新声明 (.d.ts) 文件
这是我的代码:
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 错误:
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",
}
}
编辑:尝试添加 .d.ts 文件,VS Code 错误消失但仍无法编译源代码。
这里自答,不得不搬家
ProductSchema.plugin(猫鼬,{
主持人:[
"localhost:9200"
]
});
以上
const Product = mongoose.model("Product", ProductSchema);
因为我随后在没有使用之前应用的插件的情况下使用 Product,这没有意义。
我尝试用 npm 添加 mongoosastic @types 但是没有用,我怎样才能让它识别函数? (.search() 也不起作用)
VS Code 描述的错误:
模块"c:/Users/joche/Desktop/Proyectos/E-Commerce/Backend/node_modules/mongoosastic/lib/mongoosastic"
找不到模块 'mongoosastic' 的声明文件。 'c:/Users/joche/Desktop/Proyectos/E-Commerce/Backend/node_modules/mongoosastic/lib/mongoosastic.js' 隐式具有 'any' 类型。
如果存在,请尝试 npm install @types/mongoosastic
或添加包含 declare module 'mongoosastic';
ts(7016)
这是我的代码:
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 错误:
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",
}
}
编辑:尝试添加 .d.ts 文件,VS Code 错误消失但仍无法编译源代码。
这里自答,不得不搬家
ProductSchema.plugin(猫鼬,{ 主持人:[ "localhost:9200" ] });
以上
const Product = mongoose.model("Product", ProductSchema);
因为我随后在没有使用之前应用的插件的情况下使用 Product,这没有意义。