【问题标题】:JayData with WebAPI OData V4 returns undefined带有 WebAPI OData V4 的 JayData 返回未定义
【发布时间】:2023-03-17 04:03:01
【问题描述】:

我正在 NodeJS 中编写 AWS lambda 函数以从 WebAPI OData V4 服务 (http://localhost/SomeService/$metadata) 读取数据 我使用 JaySvcUtil 生成了元数据文件,并在 Lambda 函数中引用了生成的文件

'use strict';
let $data = require('jaydata');
let meta = require('./apiData');

exports.handler = function (event, context) {

var con = new DataContext({
    name: 'oData',
    oDataServiceHost: 'http://localhost/SomeService'
});

con.onReady(function () {
    $data.Users.forEach(function(data) {
        console.log(data.Id);
    });;
};

不幸的是,当调用 onReady 方法时,用户对象以及我的 API 公开的任何其他类型都未定义。

知道我做错了什么吗?

【问题讨论】:

    标签: node.js odata asp.net-web-api2 aws-lambda


    【解决方案1】:

    原来 jaysvcutil 创建的生成代码不适用于 nodejs。 应该按照文档中的说明使用 .define,而不是使用 .extend。

    $data.Class.define('Project.User', $data.Entity, null, {
        'UserID': { 'key': true, 'type': 'Edm.Int32', 'nullable': false, 'computed': true },
    });
    
    $data.Class.define('$Project.Types.Context', $data.EntityContext, null, {
        Users: { type: $data.EntitySet, elementType: Project.User }
    });
    

    并且上下文初始化回调应该定义如下:

    jayContext = new $Project.Types.Context({
        name: 'oData',
        oDataServiceHost: 'http://localhost/SomeService'
    }); 
    
    jayContext.onReady(function (serviceContext) {
        serviceContext.Users.forEach(function (user) {
            console.log(user.UserID);
        });
    
        context.done(null, 'Hello World');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      相关资源
      最近更新 更多