【问题标题】:node RESTful API model with firebase带有 Firebase 的节点 RESTful API 模型
【发布时间】:2017-10-03 20:33:34
【问题描述】:

我正在尝试使用 Node.js 创建一个 RESTful API,它是为 Android 应用程序服务的。我正在为我的数据库使用 firebase。我正在按照教程 tutorial 创建 API。它使用猫鼬,并分别定义了路由、模型和控制器。这看起来是一个很好的关注点分离,并且希望对我的代码也遵循类似的东西。但想知道在这种情况下如何创建我的模型。我能够从 firebase 读取数据。

//到目前为止我的代码

var admin = require('firebase-admin');

var serviceAccount = require('../../<firebase.json>');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "<path_to_firebase_db",

});

var db = admin.database();
var ref = db.ref();
var item = ref.child('Item');


item.orderByChild("price").on("child_added", function(snapshot) {
  console.log(snapshot.key + " was " + snapshot.val().price + "$");
});

// 我的参考在以下链接中给出:

'use strict';

var mongoose = require('mongoose');

var Schema = mongoose.Schema;


var TaskSchema = new Schema({
  name: {
    type: String,
    Required: 'Kindly enter the name of the task'
  },
  Created_date: {
    type: Date,
    default: Date.now
  },
  status: {
    type: [{
      type: String,
      enum: ['pending', 'ongoing', 'completed']
    }],
    default: ['pending']
  }
});


module.exports = mongoose.model('Tasks', TaskSchema);

我想创建类似的模型,我该怎么做?我是 node 和 firebase 的新手,我试图找到一些资源,但到目前为止还没有成功?还是有其他方法?

【问题讨论】:

    标签: node.js rest api firebase mongoose


    【解决方案1】:

    目前,以下内容似乎对我有用。找不到任何其他类似的功能,例如用于 firebase 的 mongoose.model

    var item = ref.child('Item');
    
    item.on('value', function(snapshot){
      console.log(snapshot.key);
    
      exports.getItems = function() {
        //get the required value and export it so as to be accessed from client
        var o = {};
        var key = snapshot.key;
        o[key] = [];
        o[key].push(snapshot.val());
    
      return o;
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2018-03-08
      • 1970-01-01
      • 2018-05-19
      • 2015-03-07
      • 2018-01-20
      • 2023-02-10
      相关资源
      最近更新 更多