我试着用npm添加mongoosastic @type,但是不起作用,我怎么才能让它识别函数呢?(.search()也不起作用)
VS代码描述的错误:
模块"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的新声明(.d.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代码错误已消失,但仍无法编译源代码。
发布于 2019-11-27 09:37:26
我在这里自答,不得不搬家
ProductSchema.plugin(mongoosastic,{ hosts:"localhost:9200“});
上图
const产品=mongoose.model(“产品”,ProductSchema);
因为我使用Product时没有使用之前应用的插件,这是没有意义的。
https://stackoverflow.com/questions/59058692
复制相似问题