- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图在 javascript 中使用 mongoose 获取单个项目,但无法使其工作。该项目在我的数据库中的标题为“名称”,当我将 .get 与/products 一起使用时,它工作正常。我所有获得所有项目的方法都效果很好,我只是在处理单个项目时遇到了麻烦。任何帮助是极大的赞赏。谢谢!
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extend: true
}));
app.use(express.static("public"));
mongoose.connect("mongodb://localhost:27017/ShoppingCartDB", {useNewUrlParser: true});
const productSchema = {
name: String,
price: String,
quantity: Int
};
const Products = mongoose.model("Products", productSchema);
//sets route to all items in products database
app.route("/products")
//gets ALL items in cart
.get(function (req, res) {
Products.find(function(err, foundProducts) {
console.log(foundProducts);
if (!err) {
res.send(foundProducts);
} else {
res.send(err);
}
});
})
//deletes ALL items in cart
.delete(function(req, res) {
Products.deleteMany(function(err) {
if (!err) {
res.send("All products removed from shopping cart");
} else {
res.send(err);
}
})
});
//changes route to single item based on name
app.route("/products/:itemName")
.get(function(req, res) {
Products.findOne({name: req.params.itemName}, function(err, foundItem){
if (foundItem) {
res.send(foundItem);
} else {
res.send(err);
}
});
})
.delete(function(req, res) {
Products.deleteOne(
{name: req.params.itemName},function(err) {
if (!err) {
res.send("Success");
} else {
res.send(err);
}
}
);
});
app.listen(3000, function() {
console.log("Server is running on port 3000");
});
我的数据库:
{
"_id": "5e1ca39ef94f6e20827ee890",
"name": "orange",
"price": "0.99",
"quantity": 1
},
{
"_id": "5e1ca39ef94f6e20827ee893",
"name": "apple",
"price": "1.5",
"quantity": 2
},
{
"_id": "5e1ca39ef94f6e20827ee896",
"name": "bread",
"price": "3.99",
"quantity": 1
},
{
"_id": "5e1ca39ef94f6e20827ee899",
"name": "milk",
"price": "4.99",
"quantity": 3
},
{
"_id": "5e1ca39ef94f6e20827ee89c",
"name": "coffee",
"price": "4.99",
"quantity": 2
}
最佳答案
您的代码有两个问题:
数量应该是Number
,而不是Int
:
const productSchema = {
name: String,
price: String,
quantity: Number
};
extend: true
不是 bodyParser
上的 extend: true
:
app.use(
bodyParser.urlencoded({
extended: true
})
);
这样我就可以在演出路线上获得200
。
关于javascript - mongoose findOne() 函数不返回指定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59721587/
我有一个简单的 node.js 代码,它使用 mongoose,它在保存时工作但不检索。 .save() 有效,但 .findOne() 无效。 mongoose = require('mongoos
const categorySlug = req.query.category; const category = await Category.findOne({ slug: categorySlu
我正在使用 Waterline通过 Sails 查询 MySQL 数据库。我找到了 2 种方法。 不知道哪个更好? 顺便问一下,如何处理这两种情况的错误? 1. Model.findOne().whe
我可以在没有数据匹配的情况下将 findOne 查询结果从 NULL 更改为空对象吗?如果是,如何? 先感谢您 最佳答案 我认为此选项的 Sequelize 中没有插入配置。 您可以为模型访问层创建一
我将 NestJS 框架与 TypeORM 和 PostgreSQL 结合使用。假设我有一个简单的 User 实体,它只有两个属性:id 和 name @Entity() class User {
我将 NestJS 框架与 TypeORM 和 PostgreSQL 结合使用。假设我有一个简单的 User 实体,它只有两个属性:id 和 name @Entity() class User {
I'm practicing by following a React, MUI, Nodejs, CRUD project. Currently am able to get the regi
我正在尝试使用 Sequelize 和 mysql db 创建一个模型。我正在尝试发布到“/students/register”,它一直给我一个错误,说 findOne 不是一个函数。我尝试要求我的
有没有可能做findOne在mongodb中解释? 目前我已经尝试过: db.coll.findOne({query}).explain() db.collection.findOne({query}
在我的应用程序中,我尝试使用 .findOne 但出现错误。这是我的错误:- TypeError: User.findOne is not a function at Strategy._verify
我有一个 user.js 文件,用于我网站中的用户模型。这是用户架构: var UserSchema = mongoose.Schema({ username: { type:
我正在从 SQL 迁移到 MonboDB,并且我正在尝试学习基础知识。但到目前为止,我一直在努力解决一个“Select 语句” 假设我有这样的收藏 文档1 { "_id" : "id1", "
login: function (req, res) { var username = req.param('username'); var password = re
潜在客户和客户都是集合。为什么 Leads.findOne() 返回一个对象?它应该像 Customers.findOne() 那样返回一个 Document。 我使用的是 Meteor 1.2.1。
我的 mongodb 集合中的 ids 格式如下 "_id" : NumberLong("502434721406525440"), 我正在尝试执行 mongo 的 findOne 方法来检索具有
我专门使用 mongoose,尽管我认为这没有那么重要。例如,假设我有一个名为 MongoQueue 的集合,我向该队列添加了一些人。 `MongoQueue.save {function(err,
我是第一次使用 Mongodb,我遇到了一个非常奇怪的错误。我有一个“游戏”集合,但我无法使用 _id 查询对其进行搜索。 我直接在 mongo shell 上尝试,结果如下: > db.games.
我的问题是:有一组用户。我试图找到 _id=xxx 的用户是否有 somevalue > 5。 我想知道,使用 find(...).count() > 0 或 findOne(...) != null
我在使用 TypeORM 执行嵌套查找查询时遇到一些问题。这是基本代码: const { completionId } = req?.params; const user = req.
我尝试使用护照添加 Facebook 登录信息,但遇到以下错误: 类型错误:无法在 Strategy._verify 中调用未定义的方法“findOne” 我的 app.js 文件中需要 models
我是一名优秀的程序员,十分优秀!