如何为回铃音制作mongodb的后期API
问题描述:
我是mongodb
和loopback
的新手。我想发送数据并将数据从我的应用程序保存到数据库。我怎样才能做到这一点?如何为回铃音制作mongodb的后期API
更新 店型号:
{
"shopname": "string",
"tel": "string",
"latlng": "string",
"address": "string",
"id": "string",
"personId": "string"
}
卷曲:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
"shopname": "spring", \
"tel": "12345678", \
"latlng": "52.1106986,21.7768998", \
"address": "05-319 Skwarne, Poland" \
}' 'http://localhost:3000/api/shops'
现在我应该在shops.js
写信给一个API,使用应用程序发送数据到数据库?
'use strict';
module.exports = function(Shops) {
};
答
你应该已经提供了关于你已经完成的步骤的更多信息。 让我的第一步开始:
下载并在服务器上安装的MongoDB:link
-
运行的MongoDB后,添加您所需的数据库信息,以datasources.json文件。例如
{ "db": { "name": "db", "connector": "memory" }, "shopDB": { "host": "localhost", "port": 27017, "url": "mongodb://localhost:27017/shopDB", "database": "shopDB", "password": "", "name": "shopDB", "user": "", "connector": "mongodb" } }
附加回送连接器的MongoDB通过NPM项目。 (:在你的项目的根文件夹,你可以利用回送的用户友好的命令行界面,这样做调用命令“模式SLC回送”。)
您完成第4步,之后环
-
现在定义模型将为您创建2个文件:shop.js和shop.json =>这些文件位于您的projectFolder/common/models /目录中。请注意,按照命名模型中的环回约定并以单数形式命名模型(商店)是一种很好的做法。 (它在项目的其他部分使用复数形式的模型名称)。您shop.json应该像下面的代码:
{ "name": "shop", "plural": "shops", "base": "PersistedModel", "idInjection": true, "options": { "validateUpsert": true }, "properties": { "shopname": { "type": "string", "required": true }, "tel": { "type": "string", "required": true }, "latlng": { "type": "string", "required": true }, "address": { "type": "string" }, "personId": { "type": "string", "required": true } }, "validations": [], "relations": {}, "acls": [], "methods": {} }
现在你可以发表你的店铺JSON来http://localhost:3000/api/shops/。请注意,我们的商店模型继承自PersistedModel基本模型并具有一些内置函数来执行粗陋操作。如果你想在你的db中创建一些商店实例,你不需要添加任何东西到你的shop.js文件!
你有没有试过把它输入到谷歌? https://loopback.io/doc/en/lb2/Connecting-to-MongoDB.html –
@ p.streef是的,其实我读过这篇文章,发现我必须为CRUD编写函数。但我怎么写什么,我不清楚。 – RSA
那么,那么也许你应该在你的问题中分享?如果你想让人帮助你,你应该表现出努力。分享您尝试过并且无法使用的代码,以及为什么它无法使用。 –